How to use selection from list menu

25 Ansichten (letzte 30 Tage)
cancel077
cancel077 am 28 Sep. 2018
Bearbeitet: Walter Roberson am 4 Okt. 2018
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!!

Akzeptierte Antwort

Nicole Peltier
Nicole Peltier am 28 Sep. 2018
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 Kommentare
Nicole Peltier
Nicole Peltier am 1 Okt. 2018
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?
cancel077
cancel077 am 4 Okt. 2018
No issues on my 2nd question. I figured that one out, thank you very much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink Environment Customization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by