How to Input and Display a Matrix in Matlab?

Click For Summary
To input and display a matrix in MATLAB, users should utilize the input function to gather matrix dimensions and elements. The correct syntax involves using parentheses instead of brackets for matrix indexing, and the loop should iterate from 1 to m and n, not m-1 and n-1. The disp function is recommended for displaying the matrix, while fprintf can be used for formatted output. Users can prompt for matrix elements once before the loop to avoid repetitive messages during input. Understanding these differences is crucial for transitioning from C++ to MATLAB.
Raghav Gupta
Messages
1,010
Reaction score
76

Homework Statement


I have to make program that a user inputs a matrix and program displays it.

Homework Equations

The Attempt at a Solution


I know the logic as in c++ I am able to display that.
Here,
Matlab:
m=input('Enter rows of matrix'); % Why not double quotes here as in cout of C++?
n=input('Enter columns of matrix');
display('Enter the matrix elements');% To use what disp or display or fprintf here?
for i= 1:m-1
   for  j=1:n-1
        arr[i][j]=input(''); % Here error is coming of unbalanced parentheses or brackets.
   end
enddisplay('The matrix elements are:');
for(i=0;i<m;i++)
    for(j=0;j<n;j++)
disp(arr[i][j]);
    disp('\t');
    end
    fprintf('\n');
end
 
Last edited:
Physics news on Phys.org
Hi there,

Raghav Gupta said:
% Why not double quotes here as in cout of C++?
Matlab recognizes single quotes for input of strings.

Raghav Gupta said:
% To use what disp or display or fprintf here?

fprintf is to useful when displaying strings for which you want to control the format of the data inside your string.
disp is simply used to display a string, such as the one you typed above. Note, however, that using disp does not allow you to format your string whatsoever, and should only be used when displaying simple strings like these.
NB: There's no such function display in Matlab. Use disp.

An example of using fprintf is the following:
>> fprintf('My age is %d\n', 20)
>> My age is 20

You can type help fprintf into the command window to get more information on this. In this case, from what I gather, disp will serve your purpose just fine.

Raghav Gupta said:
arr[i][j]=input(''); % Here error is coming of unbalanced parentheses or brackets.

Here I am assuming that you want to store the user's input into a matrix element.
Do note that to call an element of a matrix by its row and column, the proper way is by doing it this way:
>> mat(r,c) = input('Please input an element: ');
where mat is the name of your matrix (it is stored as a variable in your workspace) - you have to define it first above this line of code (outside your for loop)


Raghav Gupta said:
display('The matrix elements are:');
for(i=0;i<m;i++)
for(j=0;j<n;j++)
disp(arr[i][j]);
disp('\t');
end
fprintf('\n');
end

Matlab's syntax is different from C++. Use the same format as you did for the for loop above. Use the colon operator to specify the range for i and j.
 
JeremyG said:
There's no such function display in Matlab
display is functioning as disp only.Means display is working.
After correcting the changes I am still not able to input the values in matrix. Only one value able to input.
 
What does your code look like now?
 
JeremyG said:
What does your code look like now?
Like this
Matlab:
m=input('Enter rows of matrix\n');
n=input('Enter columns of matrix\n');

for i= 1:m-1
   for  j=1:n-1
        arr(i,j)=input('Enter the element\n');
   end
enddisplay('The matrix elements are:');
for i= 1:m-1
    for j=1:n-1
      
        disp(arr(i,j));
    display('\t');
    end
    fprintf('\n');
end
 
Raghav Gupta said:
for i= 1:m-1
for j=1:n-1

just a word of note, if you want a matrix of m rows and n columns, you should be iterating from i =1:m and j=1:n

If you just want to display the matrix after the user has finished creating the matrix, you can just use the disp function.
like this:
>> disp(arr)

Raghav Gupta said:
After correcting the changes I am still not able to input the values in matrix. Only one value able to input.

I'm still not quite sure what you mean here.
 
Raghav Gupta said:
After correcting the changes I am still not able to input the values in matrix. Only one value able to input.
In your nested for loops you will be able to enter one matrix element at a time. Is that what you're asking about?
 
Mark44 said:
In your nested for loops you will be able to enter one matrix element at a time. Is that what you're asking about?
I'm asking about that the message "Enter the elements" should appear once instead of it coming in loop but elements we can enter one by one.
It's a complete different scenario here from C++ of creating matrix. How one can learn MATLAB if he knows basics of c++?

Also what is difference between disp and display here?

My MATLAB code finally
Matlab:
m=input('Enter rows of matrix\n');
n=input('Enter columns of matrix\n');

for i= 1:m
   for  j=1:n
        arr(i,j)=input('Enter the element\n'); % This message is appearing in loop every time.
   end
enddisplay('The matrix elements are:');        
        disp(arr);% No void ,main() function here as in C++,  Wow! we can simply display matrix instead of going in for loop again as in C++.
 
So you want the message "Enter the elements: " to appear once and after that allow the user to key in element by element (without any visual prompts?)

An easy solution would be to do what you did before, which is to display the message right before entering the for loop.
As for the input function, while it is possible to just type this:
>> arr(i,j) = input(' ');
It would not be visually pleasing but if that is the requirement for your assignment then go ahead.

Another friendly note:
The input function does not need a \n at the end of the string, unlike the fprintf function. This way, the user's input will be entered beside your prompt, and after he clicks enter, Matlab automatically goes to the next line.
 
  • Like
Likes Raghav Gupta
  • #10
One thing I not got was that what is the difference between disp and display in matlab. I tried both. But not able to spot the difference.
 
  • #11
Hi, there are 2 main differences between disp and display

1. disp does not print the variable name or ans

Code:
a = 1;
disp(a)

1

display(a)

a = 
     1

2. disp prints nothing for built-in types (numeric types, char, struct, and cell) when the value is empty.

Code:
>> a = {};
disp(a)
display(a)

a =

     {}

Subtle difference. Sorry for the confusion earlier
 
  • Like
Likes Raghav Gupta

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K