필터 지우기
필터 지우기

prompt user to input n matrices

조회 수: 12 (최근 30일)
Prakriti Biswas
Prakriti Biswas 2020년 5월 4일
답변: Chris Basic 2020년 5월 5일
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