prompt user to input n matrices

12 次查看(过去 30 天)
How do I prompt the user to input n matrices of 1x4 dimensions?
  1 个评论
Chris Basic
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
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'})
% 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));

Chris Basic
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

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by