필터 지우기
필터 지우기

How to use selection from list menu

조회 수: 12 (최근 30일)
cancel077
cancel077 2018년 9월 28일
편집: Walter Roberson 2018년 10월 4일
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!!

채택된 답변

Nicole Peltier
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
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?
cancel077
cancel077 2018년 10월 4일
No issues on my 2nd question. I figured that one out, thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by