How to get array user input in 1 dialog box

조회 수: 12 (최근 30일)
Ojaswi
Ojaswi 2023년 3월 20일
댓글: Jon 2023년 3월 22일
I have a set of code that creates 1 + 3 input dialog boxes to get parameters to run an analysis. I'd like to get the number of dialog boxes down to 1 + 1, where the requested inputs are aligned as 3 columns rather than 1 long series of inputs.
% get the number of levels
m = inputdlg('Enter the number of levels', 'Levels', [1 35], {'5'});
m = str2double(cell2mat(m));
%% get levels
prompt1 = repmat({'Level '}, 1, m);
for i = 1:m
prompt1{i} = [prompt1{i} int2str(i)];
end
levels = inputdlg(prompt1, 'Get Levels', [1 35] );
for i =1:m
lev(i) = str2double(levels{i});
end
levels = lev;
%% Get number of samples for each level
prompt2 = repmat({'N Level '}, 1, m);
for i = 1:m
prompt2{i} = [prompt2{i} int2str(i)];
end
reps = inputdlg(prompt2, 'Get Number of Samples', [1 35] );
for i =1:m
rep(i) = str2double(reps{i});
end
reps = rep;
%% Get runouts for each level
prompt3 = repmat({'Runouts Level '}, 1, m);
for i = 1:m
prompt1{i} = [prompt1{i} int2str(i)];
end
runouts = inputdlg(prompt3, 'Get Runouts', [1 35] );
for i =1:m
run(i) = str2double(runouts{i});
end
runouts = run;
%% Proceed with analysis after userinput
analyzeData(levels, reps, runouts);

채택된 답변

Jon
Jon 2023년 3월 20일
If I am understanding what you would like to do I think this might work.
% Use single dialog box to collect all information, user enters data as
% space separated lists of numbers
% Build the input prompt strings
prompt = ["Enter the number of levels",...
"Enter level values as space separated list",...
"Enter number of samples as space separated list",...
"Enter runouts as space separated list"]
% Display the dialog
answer = inputdlg(prompt,'Experiment Design');
% convert to numerical values
numLevels = str2num(answer{1});
levels = str2num(answer{2});
reps = str2num(answer{3});
runouts = str2num(answer{4});
The main weakness of this is that it doesn't force the user to put in the required number of inputs for each subprompt. You could at least check after the user inputs the data that the number of elements of each response was correct and ask again if it isn't. Might be annoying to user though to get an error after they already typed in a lot of numbers.
  댓글 수: 6
Jon
Jon 2023년 3월 22일
Excellent! Glad this helped.
Jon
Jon 2023년 3월 22일
which I then extended to answer your specific question

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by