Matlab GUI Tutorial - Pop-up Menu
29 Oct 2007 Quan Quach 46 comments 14875 views
Introduction
In this Matlab GUI tutorial, you will learn how to create and use the Pop-up Menu component. Pop-up menus are used as a control for choosing between a set of options. When the user clicks on the Pop-up menu, the menu expands, revealing a set of choices that the user can pick. A common use for Pop-up menus is a font size selector (shown below).

This tutorial is written for those with little or no experience creating a Matlab GUI (Graphical User Interface). If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab is not required, but recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan’t too outdated). Let’s get started!
Create the Visual Aspect of the GUI
-
First, open up Matlab. Go to the command window and type in
guide.
-
You should see the following screen appear. Choose the first option
Blank GUI (Default).
-
Click on
and add a Static Text component to the GUI figure. Next, click on
and add a Pop-up Menu component onto the GUI figure. -
Double click the Static Text component to bring up the Property Inspector. Change the String property to
Testing!!!, and change the Tag property totesting_staticTextas shown in the figure below:

You can also modify the BackgroundColor property if you desire. -
Next, let’s modify the properties of the Pop-up Menu component. First, click on the icon on the String property line as shown below. This allows you to edit the description for each option in the Pop-Up Menu.

After clicking on the icon, you should now see the following window. Fill in the window as shown below:

In addition, I set the Tag property to
popupmenu1, which is the default name. You might want to make sure that its named properly before you move on. -
Here’s what your figure should look like after you add the components and modify them.

-
At this point, you also might want to add some Static Text components to add some description tags to the GUI. You can modify their text by double clicking on the component and changing the String property. It’s not required, but I highly recommend it.

-
Save your GUI wherever you please with your desired filename.
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