Help and Support on GUIDE - Pop up menu condition push button

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

Can you attach the .fig file and the .m file so we can try it?
Dear All,
Kindly find the attached .fig and the .m file.
>> animals
Error messages displayed on running the code:
Undefined function or variable 'popupValue1'.
Error in animals>pushbutton1_Callback (line 232)
if popupValue1=='0.0-0.25' && popupValue2==K1 && popupValue3==L1
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Kindly do the needful.
Cheers.
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.
Dear All,
Thanks for your assistance.
The answer has not helped to resolve my question. I don't know what is supposed to be written under each popup menu callback (that is, popupmenu1, popupmenu2 and popupmenu3) as well as what is supposed to be written under the push button callback (that is pushbutton1). I am still getting the error messages below:
>> animals
Undefined function or variable 'popupValue1'.
Error in animals>pushbutton1_Callback (line 232)
if popupValue1=='0.0-0.25' && popupValue2==K1 && popupValue3==L1
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback.
Cheers.

Sign in to comment.

Answers (2)

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

Dear All,
Thanks for your assistance.
The answer has not helped to resolve my question. I don't know what is supposed to be written under each popup menu callback (that is, popupmenu1, popupmenu2 and popupmenu3) as well as what is supposed to be written under the push button callback (that is pushbutton1). I am still getting the error messages below:
>> animals
Undefined function or variable 'popupValue1'.
Error in animals>pushbutton1_Callback (line 232)
if popupValue1=='0.0-0.25' && popupValue2==K1 && popupValue3==L1
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback.
Cheers.
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
Dear All,
Many thanks for your time and help.
The answer has helped partially to resolve my question. But I have got some error messages which are stated below:
I have attached the animals.fig and the animals.m file for your perusal.
>> animals
Error using feval
Undefined function 'animals_OutputFcn' for input arguments of type 'struct'.
Error in gui_mainfcn (line 265)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error using feval
Undefined function 'pushbutton1_Callback' for input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Error using feval
Undefined function 'popupmenu1_Callback' for input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Error using feval
Undefined function 'popupmenu2_Callback' for input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('popupmenu2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Error using feval
Undefined function 'popupmenu3_Callback' for input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('popupmenu3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Error using feval
Undefined function 'pushbutton1_Callback' for input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Thanks.

Sign in to comment.

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

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.
Dear All,
Many thanks. I have taken the end's out. I have recreated a new GUI (animals3.fig and animals3.m respectively, both are attached to this comment) from scratch without adding any "end"s to close off the functions. But the new code is still not executable. Kindly find the new error messages below:
>> animals3
Attempt to reference field of non-structure array.
Error in animals3>pushbutton1_Callback (line 173)
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
More help is still needed.
Kindly do the needful.
Cheers.
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.
Dear All,
Many thanks for your time and assistance.
I am grateful for changing the push button1 callback to reflect edit1 rather than 'display'. But nothing is displayed in the edit1 after pressing push button1.
Each 'if' condition is to test for combination of 3 strings - each string from the 3 popupmenus, that is from popupmenu1, popupmenu2 and popupmenu3. The pushbutton1 is supposed to send the appropriate name of the animal (result) to edit1.
I could not see those lines having red squiggles under them as a result of having many of the 'if' conditions that check for the same set of numbers hence I could not correct them.
Kindly find the new error messages below:
>> animals3
Attempt to reference field of non-structure array.
Error in animals3>pushbutton1_Callback (line 173)
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
More help is still needed.
Kindly do the needful.
Cheers.
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?
Dear All,
Many thanks for your time and help.
The values are to be combined to determine an animal’s name by selecting a string from each of the 3 popup menus to form a combination. 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 pushbutton1 should assign the word “Lion” as a result (output) and send same to the edit1 control.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K1 from popup menu ‘B’ and string L2 from popup menu ‘C’, the pushbutton1 should assign the word “Parrot” as a result (output) and send same to the edit1 control.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K2 from popup menu ‘B’ and string L1 from popup menu ‘C’, the pushbutton1 should assign the word “Goat” as a result (output) and send same to the editext1 control.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K2 from popup menu ‘B’ and string L2 from popup menu ‘C’, the pushbutton1 should assign the word “Antelope” as a result (output) and send same to the editext1 control. etc.
I have had unique sets of numbers now. I am sure I ran the code you uploaded and not my original code.
On running the code, The GUIDE window was displayed but on selecting a set of three strings (one from each popup menu) and pressing the control "Combine" (a pushbutton) on it, the below error messages were displayed in the command window:
>> animals3
Attempt to reference field of non-structure array.
Error in animals3>pushbutton1_Callback (line 173)
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
More help is still needed.
Kindly do the needful.
Cheers.
Dear All,
Many thanks for your time and help.
The values are to be combined to determine an animal’s name by selecting a string from each of the 3 popup menus to form a combination of three strings and thereby assign an animal's name (as stated in the nested if statements) and thereafter display the appropriate name of the animal in the control named edit1. 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 pushbutton1 should assign the word “Lion” as a result (output) and send same to the control named edit1.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K1 from popup menu ‘B’ and string L2 from popup menu ‘C’, the pushbutton1 should assign the word “Parrot” as a result (output) and send same to the control named edit1.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K2 from popup menu ‘B’ and string L1 from popup menu ‘C’, the pushbutton1 should assign the word “Goat” as a result (output) and send same to thecontrol named edit1.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K2 from popup menu ‘B’ and string L2 from popup menu ‘C’, the pushbutton1 should assign the word “Antelope” as a result (output) and send same to the control named edit1. etc.
I have had unique sets of numbers now. I am sure I ran the code you uploaded and not my original code.
On running the code, The GUIDE window appeared but on selecting a set of three strings (one from each popup menu) and pressing the control named "Combine" (a pushbutton) on the figure named fig1. You may wish to test the .fig and the .m files for confirmation purposes.
The below error messages were displayed in the command window:
>> animals3
Attempt to reference field of non-structure array.
Error in animals3>pushbutton1_Callback (line 173)
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
More help is still needed.
Kindly do the needful.
Cheers.
Dear All,
Many thanks for your time and help.
This problem is still giving me sleepness nights without making any success. Kindly do the needful.
The values are to be combined to determine an animal’s name by selecting a string from each of the 3 popup menus to form a combination of three strings and thereby assign an animal's name (as stated in the nested if statements) and thereafter display the appropriate name of the animal in the control named edit1. 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 pushbutton1 should assign the word “Lion” as a result (output) and send same to the control named edit1.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K1 from popup menu ‘B’ and string L2 from popup menu ‘C’, the pushbutton1 should assign the word “Parrot” as a result (output) and send same to the control named edit1.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K2 from popup menu ‘B’ and string L1 from popup menu ‘C’, the pushbutton1 should assign the word “Goat” as a result (output) and send same to thecontrol named edit1.
Also, if string 0.0 to 0.25 was selected from popup menu ‘A’, string K2 from popup menu ‘B’ and string L2 from popup menu ‘C’, the pushbutton1 should assign the word “Antelope” as a result (output) and send same to the control named edit1. etc.
I have had unique sets of numbers now. I am sure I ran the code you uploaded and not my original code.
On running the code, The GUIDE window appeared but on selecting a set of three strings (one from each popup menu) and pressing the control named "Combine" (a pushbutton) on the figure named fig1. You may wish to test the .fig and the .m files for confirmation purposes.
The below error messages were displayed in the command window:
>> animals3
Attempt to reference field of non-structure array. What does this error message mean and how can someone debug it?
Error in animals3>pushbutton1_Callback (line 173)
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
More help is still needed.
Kindly do the needful.
Cheers.
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.
@Gbola tell me exactly what to do to see the error, because when I downloaded the latest animals3 files, it seemed to work.
On running the code, The GUIDE window appeared but on selecting a set of three strings (one from each popup menu) and pressing the control named "Combine" (a pushbutton) on the figure named fig1.
Although, the latest animals3 files actually worked but nothing is displayed in the edit1 control - it was blank when the pushbutton named "Combine" was pressed.
Many thanks.
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.
Kindly find the steps below:
User Comment:
"Step 1: I created the GUI using GUIDE
Step 2: I placed 3 popup menus on the figure
Step 3: I placed one edit1 control on the figure
Step 4: Il placed one push button 'Combine' on the figure
Step 5: I saved the .fig and .m files
Step 6: I ran the code and it ran
Step 7: I selected one item each from the 3 popup menus on the figure.
Step 8: I pressed the push button 'Combine' on the figure, but the name of the animal was not displayed in the edit1 control. The below error message appeared in the command window:
>> animals3
Attempt to reference field of non-structure array. What does this error message mean and how can someone debug it?
Error in animals3>pushbutton1_Callback (line 173)
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
More help is still needed.
Kindly do what's needed.
Cheers.
NB: I am using MATLAB 2012b. Kindly let me know the version of MATLAB you are using. May be the problem has to do with the MATLAB version that I am using"
Thanks.
Kindly find the steps below:
User Comment:
"Step 1: I created the GUI using GUIDE
Step 2: I placed 3 popup menus on the figure
Step 3: I placed one edit1 control on the figure
Step 4: Il placed one push button 'Combine' on the figure
Step 5: I saved the .fig and .m files
Step 6: I ran the code and it ran
Step 7: I selected one item each from the 3 popup menus on the figure.
Step 8: I pressed the push button 'Combine' on the figure, but the name of the animal was not displayed in the edit1 control. The below error message appeared in the command window:
>> animals3
Attempt to reference field of non-structure array. What does this error message mean and how can someone debug it?
Error in animals3>pushbutton1_Callback (line 173)
popupValue1 = handles.popupmenu1.Value; % Get index of selected item. 1, 2, 3, etc.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in animals3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)animals3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
More help is still needed.
Kindly do the needful.
Cheers.
NB: I am using MATLAB 2012b. Kindly let me know the version of MATLAB you are using. May be the problem has to do with the MATLAB verionn that I am using"
Thanks.
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.
Kindly find my versions.
Cheers.
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.
Chances are no one has an 11 year old version anymore.
By the way @Gbolagade Kola ADEGOKE, are you the same person as @Gbola?
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.

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Products

Release

R2012b

Asked:

on 11 Oct 2022

Commented:

Rik
on 8 Jan 2023

Community Treasure Hunt

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

Start Hunting!