MATLAB Dialog Boxes Help

9 次查看(过去 30 天)
Kerby
Kerby 2019-12-19
回答: Star Strider 2019-12-19
Hello, I am trying to create dialog boxes that will hold values for three variables. When the menu pops up, the user inputs a start value, stop value, and step value for these 3 variables. However, variable inputs will overwrite one another and the input step values will not save in an array. How do I save the step values in an array?
For example, if for variable x
Start Value: 1
End Value: 10
Step Value: 1
How do I get it so it will save the step values in an array.
See code attached below. Thanks!
choice1 = menu('Choose a variable','x','y','z');
prompt = {'Enter Start Value','Enter End Value','Enter Step Value'};
dlg_title = 'Input Variables';
num_lines = 1;
def = {'','',''};
answer = inputdlg(prompt,dlg_title,num_lines,def);
start = str2double(answer{1});
stop = str2double(answer{2});
step = str2double(answer{3});

回答(1 个)

Star Strider
Star Strider 2019-12-19
One approach:
lstprmt = {'Choose a variable'};
lstlist = {'x','y','z'};
choice1 = listdlg('PromptString',lstprmt, 'ListString',lstlist);
vrbl = lstlist{choice1}; % Variable Character
prompt = {'Enter Start Value','Enter End Value','Enter Step Value'};
dlg_title = 'Input Variables';
num_lines = 1;
def = {'','',''};
answer = inputdlg(prompt,dlg_title,num_lines,def);
start(choice1) = str2double(answer{1});
stop(choice1) = str2double(answer{2});
step(choice1) = str2double(answer{3});
I use listdlg here since the menu documentation suggests not to use menu.
This code uses the ‘choice1’ variable as an index into first the ‘lstlist’ cell array to store the chosen variable (if necesary), and then to use it as an array index for ‘start’ and the others. This way, the values are stored in an array, and in a position that can be used later with respect to each variable.

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by