# prompt user to input n matrices

24 views (last 30 days)
Prakriti Biswas on 4 May 2020
Answered: Chris Basic on 5 May 2020
How do I prompt the user to input n matrices of 1x4 dimensions?
##### 1 CommentShowHide None
Chris Basic on 4 May 2020
One way you could do this is have a for loop and have the user enter the dimensions:
for i=1:4
n(i)=input('Enter dimension:')
end

Sign in to comment.

### Answers (2)

Adam Danz on 4 May 2020
Edited: Adam Danz on 5 May 2020
The input function is highly flexible which means that the user can enter virtually anything that will satisfy the command such as
• 1:4
• 1 9 0 2
• [1 4 0 3]
• {6 3 0 4}
• rand(2,2)
• '3' '4' '8' '1'
• 1:20
• " 1 2 3 4"
• 1234
Some of those inputs are not what you're asking for and other inputs would require additional processing to remove unnecessary characters.
Instead, use a more constained method that forces the user to enter exactly 4 values or exacly a nx4 matrix. Here are some examples that you can apply to your needs. Neither of them fully constrain the user to the instructions but they are all an improvement upon a simple input() function.
% INPUT DIALOG: % https://www.mathworks.com/help/matlab/ref/inputdlg.html
inputdlg({'V1','V2','V3','V4'})
% UITABLE: https://www.mathworks.com/help/matlab/ref/matlab.ui.control.tableappd-properties.html
figure(); uitable('Data', nan(4,4), 'RowName', {'V1','V2','V3','V4'}, 'ColumnWidth', {60}, 'ColumnEditable', true)
You can easily make a small GUI that creates a numeric input box for each value of each vector. This one requires a little more work to set it up but it will require much less work when checking that the user obeyed the instructions. You can add instuctions and labels to the GUI and make other modifications.
f = uifigure();
x = linspace(60, f.Position(3)*.65, 4); % number of values per vector
y = linspace(60, f.Position(4)*.65, 4); % number of vectors.
[xx,yy] = ndgrid(x,y);
h = arrayfun(@(i)uieditfield(f, 'numeric', 'Position', [xx(i),yy(i), 50, 20]),1:numel(xx));
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Chris Basic on 5 May 2020
One way you could do this is have a for loop and have the user enter the dimensions:
for i=1:4
n(i)=input('Enter dimension:')
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by