Matlab GUI Tutorial - Pop-up Menu
29 Oct 2007 Quan Quach 46 comments 14877 views
Writing the Code for the GUI
Matlab automatically generates an .m file to go along with the figure that you just put together. The .m file is where we attach the appropriate code to the callback of each component. For the purposes of this tutorial, we are primarily concerned only with the callback functions. You don’t have to worry about any of the other function types.
-
Open up the .m file that was automatically generated when you saved your GUI. In the Matlab editor, click on the
icon, which will bring up a list of the functions within the .m file. Select popupmenu1_Callback.

Add the following code to the function:
%gets the selected option switch get(handles.popupmenu1,'Value') case 1 set(handles.testing_staticText,'FontSize',8); case 2 set(handles.testing_staticText,'FontSize',10); case 3 set(handles.testing_staticText,'FontSize',12); case 4 set(handles.testing_staticText,'FontSize',14); case 5 set(handles.testing_staticText,'FontSize',16); otherwise end
-
Lets quickly go over the code now. The following line of code gets the option that the user selected. Remember that in the visual layout of the GUI, we designated five different font sizes for the Pop-up Menu component, giving us five different options for the Pop-up Menu. So for example, if the user selected a font size of 8 (which was the first option in the Pop-up Menu), then the following line of code would return a value of 1. If the user selected a font size of 10, then the value returned would be 2, and so on.
get(handles.popupmenu1,'Value')
Depending on the option selected, the font of the Static Text component will be adjusted using the following line of code for each case statement:
%where ## is the appropiate fontsize value set(handles.testing_staticText,'FontSize',##);
-
Save your m-file!
Run and Test the GUI
Now that we’ve completed both the visual and code aspects of the GUI, its time to run the GUI to make sure it works.
-
From the m-file editor, you can click on the
icon to save and run the GUI. Alternatively, from the GUIDE editor, you can click on the
to launch the GUI. The following GUI should appear once you click the icon: 
-
Go ahead and try selecting different font sizes. If everything was done correctly, you should see the font size of the sample text change accordingly.
-
And that’s it. Those are the basics of using a Pop-up Menu component. You can explore the other options that the slider has to offer through the Property Inspector.
This is the end of the tutorial.
Source files can be downloaded here.
Pages: 1 2
46 Responses to “Matlab GUI Tutorial - Pop-up Menu”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

Hi there, one quick question?
why did you add the code to the slider1_Callback instead of popupmenu callback?
Hello Bode,
Thanks for catching the error. It’s supposed to be popupmenu1_Callback! I have edited the page to reflect this.
Quan
Thanks!
Thanks for the tutorial about pop up menu, but I want to ask you one question, how to show the a set of options in pop up menu from one field in table ( database ) , example : I use Ms access.
Thankyou
thanks
Hi Quan Quach
Thanks, it helps me too much
Hi there,
How would i go about changing the actual text for each font selected? i.e. when font 8 is selected from the drop down, the static text box will say “font 8 is called fred”, when font 10 is selected, “font 10 is called bill” etc etc
Thanks
Froggy,
You would change the string property to have it reflect what you want it to say.
See step five of this tutorial. Instead of putting in numbers, put in what you want the menu to say.
This is not my question Quan, I mean the drop down menu stays the same but the static box changes text depeding on what font you select, so when I choose a different font you will get different text or words unique to that font such as font 8 displays fred in the static box, font 10 displays bill in the static box, font 12 displays roger in the static box. However, if you simply change the string properties of the static box (so in this case you have five different texts for each of the five fonts), then how can you link them to the drop down menu?
Try this:
Hey there,
If i have values in the pop-up menu, how do i save whatever the user selects in a variable that will be used to create a graph when i click on a push button in my GUI. Do you have any examples that relates to that?
TQ
Thanks Quan, this has really helped!! : )
Hy Quan and Daniel,
What if I want to plot different value in the same axis according to the pop-up menu?
I already tried to use your code and using plot(_,_) instead of set(), but nothing changed.
Thank you for your help…
I have a question. Can matlab do such a thing like - when you enter a number to “edit text” component, let us say number 10, then it appears 10 new edit boxes, and then you can do lots of stuff with those newly appeared edit boxes? If there is a tutorial on that, i would like to find it.
Hi mini,
that is possible but requires that you use lines of codes to create the GUI components. It’s a little bit more complicated, and I’m sorry to inform you that we haven’t yet covered that here at blinkdagger. I recall Doug over at mathworks covered that issue, you should give it a look. You might have to do some browsing into his older posts.
Quan
thanks
Hi there Quan. I got one question for you.
If I got a pop up menu that contains 2 menu, how do I send those string value to another function. For example:
function filter_popup_Callback(hObject, eventdata, handles)
switch get(handles.filter_popup,’String’)
case ‘Unsharp’
set(handles.linear_pushbutton,’String’,'unsharp’);
case ‘Average’
set(handles.linear_pushbutton,’String’,'average’);
end
function linear_pushbutton_Callback(hObject, eventdata, handles)
I = getimage(handles.axesBefore);
h = fspecial(’sobel’);
I2 = imfilter(I,h);
Let’s say I’ve choosen Unsharp menu from the pop-up, how do I replace the ’sobel’ in the linear_pushbutton to ‘unsharp’?
Thank you
Danny,
The following should work
Well Quan, thank you for that. After I’ve done the modification, MATLAB provides me with this error… Can you help me out with this? Thank you so much. This is the error that comes out at the MATLAB when the GUI is being run.
??? Error using ==> ImageEnhancement(’linear_pushbutton_Callback’,gcbo,[],guidata(gcbo))
Function FSPECIAL expected its first input, TYPE,
to be one of these types:
char
Instead its type was cell.
Oh yea… they gave out this error too… Its from the pop up menu
??? Error using ==> ImageEnhancement(’filter_popup_Callback’,gcbo,[],guidata(gcbo))
SWITCH expression must be a scalar or string constant.
??? Error while evaluating uicontrol Callback
Hey danny,
you’re right, i gave you some faulty code. I didn’t have matlab on the pc so I couldn’t double check my answer. I’ll get back to you on this one.
Quan
Ok Quan
hello Danny,
this code should work. i tested it on matlab!
sorry for the delay
Hi there Quan,
I really appreciate for all those codes that you’ve showed me. It really helped me a lot. Thank you very much.
hola
quiero saber como colacar comandos en el pop up menu es que estoy haciendo un convertidor de unidades, y no se bien como utilizarlo
You are the most awesome online tutor ever…
May god bless you with more knowledge.
Hi
My problem to call popup menu is:
I have created in my gui a pop-up window “Direction” with
3 menues (AP,RL and FH) that must shows the direction of
medical image in 3 direction. When the user chooses it. I
tried to call the function. This pop up menu is not active.
It shows only the first or default image. What can I do to
solve this problem? and I tried to plot the image in case of popup as showen. But I get only deufalt image. my popup button has no function
==================================================
function plot_popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to plot_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,’String’) returns plot_popupmenu contents as cell array
% contents{get(hObject,’Value’)} returns selected item from plot_popupmenu
%##########################################################################
%get the value of the current Pop-up menu selection
%plotType =1, if AP option is chosen
%plotType =2, if FH option is chosen
%plotType =3, if RL option is chosen
velocityDirection = get(handles.plot_popupmenu,’Value’)%get(hObject,’Value’);
%val = get(hObject,’Value’);
str = get(hObject, ‘String’);
switch str{velocityDirection};
case ‘AP’
handles.current_data = handles.phaseImageAP;
case ‘FH’
handles.current_data = handles.phaseImageFH;
case ‘RL’
handles.current_data = handles.phaseImageRL;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get(handles.plot_popupmenu,’Value’)
axes(handles.axes2);%gives focus to second axes
if velocityDirection ==1
imshow(handles.phaseImageAP,[]);
colormap(gray);
zoom on;
else if velocityDirection == 2
imshow(handles.phaseImageFH,[]);
colormap(gray);
zoom on;
else if
imshow(handles.phaseImageAP,[]);
colormap(gray);
zoom on;
handles.current_data = handles.phaseImageAP;
axes(handles.axes2);%gives focus to second axes
imshow(handles.current_data,[]);
colormap(gray);
zoom on;
end
guidata(hObject,handles)
hi
pleas can you help me to set the data in the pop menu by a vector because all tutorial shown that the dat is defined manualy and can not be edit or set automatically.
Hello,
I need help with something. I have a pop-up menu in my gui and I have added several values that can be selected from the user. I want to be able to take the value selected as a number(eg a 2) and be able to use this in another m-file (my gui’s engine). Any ideas?
Hello Quan,
This tutorial helps me somewhat, but I there is one problem I’m having. Is there an easy way to add new items to the popup menu after the GUI is running.
For example, The GUI starts out with Zero items in the popup menu. The user has the ability to type text in a text box then hit a button to add that text to the popup menu list.
Thanks in advance.
Hi, I have essentially the same question as Sam above: how do we set up the switch cases for a dynamically generated pop-up list, after the GUI is running?
Specifically in my GUI, the user selects a file, which has a variable number of signal recordings, whose names are copied into the pop-up menu; how can I perform actions on those different signals by selecting them from the pop-up menu?
Thanks in advance!
Hi Quan,
I am writing a program to predict deflections of cantilevered and simply supported beams, under either single point, or uniformly distributed loads (UDL).
I wish to use a pop-up menu to give the user a choice of material of the beam (including an option for the user to enter their own value (young’s modulus)), and another to choose the cross-section of the beam.
What i would like to know is how would you put in an equation so that if the user selected, for example a rectangular cross section, the program would know to sub values that the user entered via edittext boxes, into that particular equation, and then take the resulting value and place it in a final equation that would calulate the deflection of the beam.
Hi every body.
I am Vietnameses. I am learning processing image with Matlab. I need help with something:
I had a Axes to display image with name is axes_image. And now , I want setup a pop-up menu with string is colors pink, gray, hsv to processing this image(image in axes_image). I want use function pink, gray, hsv of Matlab to process. But I can’t. Please help me. I write this code but it don’t run.
Val = get(handles.popupmenu1, ‘value’);
Str = get(handles.popupmenu1,’string’);
switch str(val)
case ‘gray’
handles.current_data = handles.x;
case ‘hsv’
handles.current_data = handles.x;
case ‘pink’
handles.current_data = handles.x;
get(handles.popupmenu1,’value’);
axes(handles.axes2);
if Val ==1
imshow(handles.x,[]);
colormap(gray);
else if Val == 2
imshow(handles.x,[]);
colormap(hsv);
else Val == 3;
imshow(handles.x,[]) ;
colormap(pink);
end
Thank you very much!
Using iscell and ischar in Matlab 2007b, my PopUpMenuCallback is returning a character array instead of a cell array when doing get(hObject,’String’).
Instead of using a cell array as you indicated in your code sample from 06 Apr 2008 at 10:40 pm 23 (using the curly brackets), I had to do the following:
function PopUpMenu_Callback(hObject, eventdata, handles)
val = get(hObject,’Value’)
string_list = get(hObject,’String’)
selected_string = string_list(val,1:end)
Has there been a change in Matlab or is there something screwy with my system?
Thank you
I can’t say for sure, but if there is only one string in the string list, then the data type is a “string”. If there are multiple strings in the pop up menu then it would be a “cell”. I don’t believe anything has changed between versions, so I will have to check.
Quan
Between your reply and further reading, it finally dawned on me that depending upon how the strings are set in the popup menu dictates the method used for reading them. see the two example below
using a string vector
function popUpMenu_Callback(hObject, eventdata, handles)
set(hObject,’String’,['one|two|three'])
val = get(hObject,’Value’)
string_list = get(hObject,’String’)
selected_string = string_list(val,1:end)
using a cell array of strings
function popUpMenu_Callback(hObject, eventdata, handles)
set(hObject,’String’,{’one’ ‘two’ ‘three’})
val = get(hObject,’Value’)
string_list = get(hObject,’String’)
selected_string = string_list{val}
that is good to know! thanks pwilson!
hi fine
hello,
I found your tutorials very helpful, but i still have a question:
let’s say i have 2 popup menus. How should i proceed if the options in the second menu depend on the choice in the first menu?
ex: menu1 = 1, 2 or 3
and menu2 = 11, 12 or13 if menu1 = 1
menu2 = 21, 22, 23 or 24 if menu1 = 2
and so on ?
Thanks in advance.
L.
Lauren,
Assuming you have two popupmenus named popupmenu1 and popupmenu2:
Thank you. I’ll try this.
hello
Quan
I also found your tutorials very helpful, but i still have a question:
if my popup contain string for example
c:\fdfd\hjhjh
c:\fd\hg
d:\ah\gh
and i want to do some processing in other function and add new item to popupmenu example
e:\af\aj
how can i do that
thank you
ahmed
ahmed,
You can try the following:
Hope that helps
Quan
Oh thank you Quan
yes it work good
thank you v.much for your help
ahmed