How to Input and Display a Matrix in Matlab?

Click For Summary

Discussion Overview

The discussion revolves around how to input and display a matrix in MATLAB, focusing on coding practices, syntax differences from C++, and the functionality of specific display functions. Participants address issues related to user input, matrix element storage, and the appropriate use of display functions in MATLAB.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions the use of double quotes for strings in MATLAB, noting that single quotes are recognized.
  • Another participant explains the differences between the functions disp and fprintf, suggesting that disp is suitable for simple strings while fprintf allows for formatted output.
  • There is a claim that the function display is equivalent to disp, which is contested by another participant who states that display does not exist in MATLAB.
  • Participants discuss the need to iterate correctly through matrix indices, with suggestions to adjust loop ranges to include all intended elements.
  • One participant expresses confusion about the input process, noting that they can only input one value at a time and seek a way to prompt for all elements at once.
  • Another participant suggests displaying the prompt for entering elements outside the loop to avoid repetitive messages during input.
  • There is a clarification about the differences between disp and display, with examples illustrating their behavior with variable outputs.

Areas of Agreement / Disagreement

Participants express differing views on the existence and functionality of the display function in MATLAB, with no consensus reached on this point. There is also ongoing discussion regarding the best practices for user input and display, indicating a lack of agreement on the optimal approach.

Contextual Notes

Some participants note that MATLAB's syntax differs significantly from C++, which may impact how users familiar with C++ approach matrix creation and display in MATLAB. There are also unresolved questions about the effectiveness of certain input prompts and display methods.

Who May Find This Useful

This discussion may be useful for students learning MATLAB, particularly those with a background in C++ who are trying to understand the differences in syntax and functionality related to matrix operations and user input.

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 count 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 count 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   Reactions: 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   Reactions: Raghav Gupta

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · 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