1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab matrix input program

  1. Nov 21, 2015 #1
    1. The problem statement, all variables and given/known data
    I have to make program that a user inputs a matrix and program displays it.

    2. Relevant equations

    3. The attempt at a solution
    I know the logic as in c++ I am able to display that.
    Code (Matlab M):

    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.

    display('The matrix elements are:');
    Last edited: Nov 21, 2015
  2. jcsd
  3. Nov 21, 2015 #2
    Hi there,

    Matlab recognizes single quotes for input of strings.

    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.

    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)

    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.
  4. Nov 21, 2015 #3
    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.
  5. Nov 21, 2015 #4
    What does your code look like now?
  6. Nov 21, 2015 #5
    Like this
    Code (Matlab M):

    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');

    display('The matrix elements are:');
    for i= 1:m-1
        for j=1:n-1
  7. Nov 21, 2015 #6
    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)

    I'm still not quite sure what you mean here.
  8. Nov 21, 2015 #7


    Staff: Mentor

    In your nested for loops you will be able to enter one matrix element at a time. Is that what you're asking about?
  9. Nov 21, 2015 #8
    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
    Code (Matlab M):

    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.

    display('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++.
  10. Nov 21, 2015 #9
    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.
  11. Nov 22, 2015 #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.
  12. Nov 22, 2015 #11
    Hi, there are 2 main differences between disp and display

    1. disp does not print the variable name or ans

    Code (Text):
    a = 1;



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

    Code (Text):
    >> a = {};

    a =

    Subtle difference. Sorry for the confusion earlier
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Matlab matrix input program
  1. MATLAB input question (Replies: 2)

  2. Matlab Programming (Replies: 5)