How to get array user input in 1 dialog box

11 views (last 30 days)
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);

Accepted Answer

Jon
Jon on 20 Mar 2023
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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Install Products in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!