prompt user to input n matrices
24 ビュー (過去 30 日間)
古いコメントを表示
How do I prompt the user to input n matrices of 1x4 dimensions?
1 件のコメント
Chris Basic
2020 年 5 月 4 日
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
回答 (2 件)
Adam Danz
2020 年 5 月 4 日
編集済み: Adam Danz
2020 年 5 月 5 日
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'})
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290289/image.png)
% 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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290290/image.png)
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));
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290298/image.png)
0 件のコメント
Chris Basic
2020 年 5 月 5 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!