Help and Support on GUIDE - Pop up menu condition push button
Show older comments
Dear All,
I am creating a GUI to display the result (output) of combining three strings, each string from the 3 popup menus namely ‘A’, ‘B’ and ‘C’ with GUIDE.
The popup menu ‘A’ contains four strings: 0.0 to 0.25, 0.25 to 0.5, 0.5 to 0.75 and 0.75 to 1.0; popup menu ‘B’ contains the following 4 strings: K1, K2, K3 and K4 while popup menu ‘C’ contains the following 4 strings: L1, L2, L3 and L4.
I also have one static text ‘Display’ for displaying the output (result) of the combination of the 3 strings, selecting one string each, from the 3 popup menus.
Lastly, I have one condition pushbutton ‘Combine’ which function is to send the result of combining a set of 3 strings to the static text ‘Display’, after pressing the condition pushbutton which will use a series of nested If-elseif-else-end statements to test each of the conditions in order to determine and display an appropriate output (result) for each combination of the three strings.
For instance, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K1 from popup menu ‘B’ and string L1 from popup menu ‘C’, the popup menu could send a word, e. g. Lion or any other word as a result (output) to the static text ‘Display’.
Also, if string 0.25 to 0.5 was selected from popup menu ‘A’, string K2 from popup menu ‘B’ and string L2 from popup menu ‘C’, the popup menu could send a word, e. g. Elephant or any other word as a result (output) to the static text ‘Display’.
Also, if string 0.5 to 0.75 was selected from popup menu ‘A’, string K3 from popup menu ‘B’ and string L3 from popup menu ‘C’, the popup menu could send a word, e. g. Tiger or any other word as a result (output) to the static text ‘Display’.
Also, if string 0.75 to 1.0 was selected from popup menu ‘A’, string K4 from popup menu ‘B’ and string L4 from popup menu ‘C’, the popup menu could send a word, e. g. Leopard or any other word as a result (output) to the static text ‘Display’.
Summarily, I have 3 popup menus ‘A’, ‘B’ and ‘C’, 1 static text ‘Display’ and 1 condition pushbutton ‘Combine’ on a figure (figure1).
Kindly do the needful.
Cheers.
4 Comments
Image Analyst
on 11 Oct 2022
Can you attach the .fig file and the .m file so we can try it?
Gbola
on 16 Oct 2022
Rik
on 22 Oct 2022
You should not use == to compare two char vectors. That is probably what is going wrong here.
What you also might want to reconsider is your use of GUIDE. Fully building your GUI from code makes you have much more control and flexibility.
Also, please do not post duplicate questions.
Gbola
on 25 Oct 2022
Answers (2)
Image Analyst
on 22 Oct 2022
Do not do this:
popupValue1 = get(popupmenu1.handles,'value');
It's handles that comes first, not the control name. So it should be this (and switching to OOP style).
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
and check the index of the item you want, not the string. You had this originally:
if popupValue1==1 && popupValue2==1 && popupValue3==1
However, the indexes you want start with number 2 since the index = 1 are just things like "Select M".
3 Comments
Gbola
on 25 Oct 2022
Image Analyst
on 26 Oct 2022
Why are you not using the code I gave you? For example instead of
if popupValue1==1 && popupValue2==1 && popupValue3==1
you're doing
if popupValue1=='0.0-0.25' && popupValue2==K1 && popupValue3==L1
like I said, make the values the Value of the popup, and compare them to numbers, not strings. Every callback where you have that if block, you should get all popup values directly using the known name of the popup control:
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
popupValue2 = handles.popupmenu2.Value; % Get index of selected item. 1, 2, 3, etc.
popupValue3 = handles.popupmenu3.Value; % Get index of selected item. 1, 2, 3, etc.
if popupValue1==1 && popupValue2==1 && popupValue3==1
Image Analyst
on 30 Oct 2022
0 votes
It looks like you added an "end" statement at the end of every function. You should not do that. Take most of your end's out. If you can do that, then just recreate the GUI from scratch but don't add any "end"s to close off the functions.
19 Comments
Rik
on 31 Oct 2022
Just to clarify: this is the convention GUIDE uses. Outside of that, it is generally recommended to close all functions with an end statement.
The most important thing is to make sure you do not mix the two styles.
Gbola
on 6 Nov 2022
Image Analyst
on 6 Nov 2022
You don't have a control named display. You called the edit field edit1. So I changed the push button1 callback to reflect that. See attached. However you have many of the is conditions that check for the same set of numbers so you need to correct that. Those lines will have red squiggles under them.
Gbola
on 8 Nov 2022
Image Analyst
on 8 Nov 2022
I don't understand how the values are to be combined to determine the animal name. The items in the drop down lists have numbers and things like L1, K1, etc. Here is the callback:
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
popupValue2 = handles.popupmenu2.Value; % Get index of selected item. 1, 2, 3, etc.
popupValue3 = handles.popupmenu3.Value; % Get index of selected item. 1, 2, 3, etc.
if popupValue1==1 && popupValue2==1 && popupValue3==1
handles.edit1.String = 'Lion';
elseif popupValue1 == 1 && popupValue2==1 && popupValue3==2
handles.edit1.String = 'Parro';
elseif popupValue1 == 1 && popupValue2==2 && popupValue3==1
handles.edit1.String = 'Goat';
elseif popupValue1 == 1 && popupValue2==2 && popupValue3==2
handles.edit1.String = 'Antelope';
elseif popupValue1 == 1 && popupValue2==1 && popupValue3==1
handles.edit1.String = 'Dog';
elseif popupValue1 == 1 && popupValue2==1 && popupValue3==2
handles.edit1.String = 'Tiger';
elseif popupValue1 == 1 && popupValue2==2 && popupValue3==1
handles.edit1.String = 'Leopard';
% else popupValue1==1 && popupValue2==2 && popupValue3==2
else
handles.edit1.String = 'Elephant';
end
The 4th, 5th, and 6th elseif's are checking sets of numbers that occurred earlier. For example if the values are 1,1,1 you are assigning 'Lion' but that means it will never get to the elseif where you are going to assign 'Dog'. Similar for the other two elseif's. You need to have unique sets of numbers. Other than that the code runs with no errors. Are you sure you ran the one I uploaded and not your original code?
Gbola
on 13 Nov 2022
Gbola
on 15 Nov 2022
Gbola
on 25 Dec 2022
Rik
on 25 Dec 2022
The error means that the code is trying to access a field of a variable, but that variable turns out not to be a struct.
Your version of Matlab is too old for this syntax. You will need to use get(handles.popupmenu1,'Value') instead.
Image Analyst
on 26 Dec 2022
@Gbola tell me exactly what to do to see the error, because when I downloaded the latest animals3 files, it seemed to work.
Gbola
on 26 Dec 2022
Image Analyst
on 27 Dec 2022
It displays for me:

Are you using Windows? If so, type "steps" into the "type here to search" field on the taskbar and start Steps Recorder. Start recording and capture all the steps you did. Then stop recording and attach the zip file here so I can see exactly what you did.
Image Analyst
on 27 Dec 2022
Moved: Voss
on 28 Dec 2022
I didn't need to know the first 7 steps you took to create the .fig and .m file. It doesn't matter. I just needed to know what you did at run time, like your Step 8:
I'm running r2022b and it works fine. Were you able to run the Steps recorder? I did and see the attached zip file. you can see it ran fine.
I'm beginning to think you're not running the same files I am. Please attach your versions.
Gbolagade Kola ADEGOKE
on 7 Jan 2023
Dear All,
Kindly find the attached files "animals3.m" and "animals3.fig" created with MATLAB R2022b version.
I am running MATLAB R2012b. Could you or someone else please convert the code in "animals3.m" created with MATLAB R2022b version to what MATLAB R2012b version can run?
Thanks.
Image Analyst
on 7 Jan 2023
Chances are no one has an 11 year old version anymore.
Rik
on 8 Jan 2023
I will not even attempt this conversion, nor do I expect anyone else will. This is an important reason why you should not use GUIDE. It is a mess to keep compatible, even if all the functions are compatible. I know, since I try to ensure my code on the File Exchange is compatible with most releases.
Recently I had some trouble starting older releases, so I might not even be able to help you in the first place.
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!