MATLAB Answers

Inputdlg requesting input to provide an output of 6 integers as a row vector

조회 수: 4(최근 30일)
I want to create a row of integers starting by asking an input from a user using the inputdlg function. I just need to make sure the output is a row vector with 6 integers.

채택된 답변

Stephen Cobeldick
Stephen Cobeldick 19 Feb 2018
편집: Stephen Cobeldick 19 Feb 2018
Do NOT use str2num! Using sscanf is faster than str2num (and avoids the internal eval call that str2num hides inside):
>> c = inputdlg('Enter six space-separated integers:')
>> v = sscanf(c{1},'%d',[1,Inf])
v =
1 2 3 4 5 6
To ensure that there are six integers either add an assert:
assert(numel(v)==6,'Input exactly six integers.')
or a while loop:
v = [];
while numel(v)~=6
c = inputdlg('Enter six space-separated integers:');
v = sscanf(c{1},'%d',[1,Inf])
end

추가 답변(1개)

ES
ES 19 Feb 2018
inputdata = [];
while(~isequal(size(inputdata), [1,6]))
x = inputdlg('Enter 6 integers as space-separated numbers:',...
'Sample', [1 50]);
inputdata = str2num(x{:});
end

Community Treasure Hunt

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

Start Hunting!

Translated by