If statement involving GUI criterias

1 view (last 30 days)
Hello everyone,
I am building a GUI and I am not able to execute a case in the execution.
I have an uipanel in which there is a radiobutton and a popupmenu( those 2 button are linked somehow). When clicking on the radiobutton I am able to select 3 options in the popupmenu, where there are 3 strings( X-ALL, X - GOODS, X- SERVICES) in my popupmenu.
so i wrote a get eventdata, to get the tags of each radiobutton then I used the switch in order to differentiate between the cases
the radiobutton connected to the popupmenu has a value of 1.
so my IF statement is
if get(handles.radiobutton4,'Value') == 1 & strcmp('X - All', get(handles.popupmenu1,'String'))
msgbox('test')
else
msgbox('KO')
end
I am getting KO.
I know there is an issue on this part of the statement strcmp('X - All', get(handles.popupmenu1,'String'))
because this gives an array as follows :
1 0 0
so my question is how to make the the statement take only the first element of the 'String Array' and compare it with the value of radiobutton...
Itried str2num but it didnt work...
Thank you very much
D

Accepted Answer

Image Analyst
Image Analyst on 1 Sep 2014
Don't do this: strcmp('X - All', get(handles.popupmenu1,'String'))
Like you said, you get a vector of how the 'X - All' string matches each of the 3 strings in the popup. What get(handles.popupmenu1,'String') actually gives you is a cell array of all the strings in the popup , not the text of the one item you selected.
What you should do is to know what item 'X - All' occurs at. Let's say it's number 3. Then you use value, not string:
selectedItem = get(handles.popupmenu1, 'value');
if get(handles.radiobutton4,'Value') == 1 && selectedItem == 3
If the items may be at unknown locations (3 sometimes, 2 other times, etc.) then you need to use strcmp but you need to get the string item, not the logical selection vector
allStrings = get(handles.popupmenu1, 'String');
selectedValue = get(handles.popupmenu1, 'value');
selectedString = allStrings{selectedValue};
Again, the first chunk of code is preferred if the items are always at the same number in the list, while the second one is more flexible but more complicated.
  1 Comment
Davin
Davin on 1 Sep 2014
Thanks. Your answer seems a more constructive one, replacing && with | i guess is more a workaround.
cheers
d

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!