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

2 Stimmen

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

You are awesome thank you! I kept using 'list' as my index thinking that the list will give me the index, not realizing that it is 'indx' instead! I have a few follow up questions to this:
1. My alternate solution to this was to use case but with the choice menu syntax. I.e.:
choice = menu(message,opt1,opt2,...,optn).
The matlab page on this specifically says (Not recommended). Is there a reason to this?
2. Your second code that you provided me is very appreciative, because there is a chance that I will use more than one selection. The issue using this method at the moment, is that each of the script "S1_H, S2_H2, etc.) is identical, but just has different parameters. So when I run S1_H, it will calculate what I need it to calculate and will output specific parameters. i.e. a1, a2, a3, a4, b1, etc. When I run S2_H2, it will provide me with different output but it will use the same named strings (a1, a2, a3, a4).
So when I run more than one, only one set of output will show as a1, a2, a3 are named the same.
My question how do I 'index' these sets differently, so I will be able to see both a1s? The issue is that I cannot rename them, because I am using a single equation/function that requires those variables to be named a1, a2. etc.
Thank you again, very appreciated!
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 Hilfe-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