How to use selection from list menu
27 次查看(过去 30 天)
显示 更早的评论
Hello all!
I have a simple gui menu to select a few options (in this case: H, H2 and H20). When I select the options, I would like to run the respective script file.
For instance - If I select 'H' I want it to run the S1_H.m script file, if I select H2 I want it to run S2_H2.m file and etc.
My code is as below, it 'seems' to work for first selection (which is H) but not the rest. When I select the 2nd and 3rd option, it still runs the first script file. What am I doing wrong?
I added the following line (species = vertcat(ListString);) to try to convert the cell array so I can use the values, but it seems that I get an error without it saying that "Conversion to logical from cell is not possible.", when I try and use list(1,1) instead.
list = {'H','H2','H20'};
[indx,tf] = listdlg('ListString',list);
species = vertcat(ListString);
if species (1,1)
S1_H
elseif species (1,2)
S2_H2
elseif species (1,3)
S3_H20
end
thank you in advance!!
0 个评论
采纳的回答
Nicole Peltier
2018-9-28
That vertcat line should produce an error because ListString is not the name of a variable (rather, it's a parameter that you pass in the previous line). Regardless, you don't need to convert the cell array to anything because you already have the index of the selected file in indx.
Do you ever expect more than one option to be selected in the GUI? If not, I would recommend replacing the if statements with a switch statement, like this:
list = {'H','H2','H20'};
[indx,tf] = listdlg('ListString',list);
% Call different functions depending on user's selection
switch indx
case 1
S1_H;
case 2
S2_H2;
case 3
S3_H20;
end
If the user may select more than one option, then if statements would be more appropriate. If more than one option is selected, then more than one of the functions will need to be called. This means that each string must be checked for in a separate if statement (no elses).
list = {'H','H2','H20'};
[indx,tf] = listdlg('ListString',list);
% Check whether each string was selected in GUI, run functions accordingly.
% This setup allows user to select more than one string.
if sum(indx==1)>0
S1_H;
end
if sum(indx==2)>0
S2_H2;
end
if sum(indx==3)>0
S3_H20;
end
Hope this helps!
3 个评论
Nicole Peltier
2018-10-1
1. The function menu is not recommended because it's outdated and the newer function listdlg is better. Using newer functions when they're available is better because they have better capabilities and sometimes newer releases don't recognize the old functions.
2. I can't visualize what you're talking about. Can you post your code?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Environment Customization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!