Matlab GUI - Adding a Custom Menu Bar
04 Feb 2008 Quan Quach 14 comments 3,672 views
Introduction
A menu bar can add a lot of functionality to a GUI without taking up much space. If we used a pushbutton to assign each individual callback, then the GUI would look crowded and confusing. The default menu bar can be added to a GUI quite easily, and looks like this:

Unfortunately, many of the features in the default menu bar might not apply to your GUI. Thus, the next best thing is to define your own custom menu bar. I prefer to use a custom menu bar to lump together functions that while important, are not used often enough to warrant its own pushbutton. Creating a custom menu bar is quite easy using GUIDE, and we shall illustrate this in the upcoming example.
The Default Menubar
There are two different ways you can use to add the default menu bar to your GUI.
-
Add the following line of code to the opening function:
set(handles.figure1,'MenuBar','figure');
-
Double click on the background of the GUIDE figure to bring up the Property Inspector. Set the MenuBar property to “figure”.

Adding your own Custom Menu Bar: A Simple Example
-
In this example, we will create a GUI that can adjust the size of the text using the menu items created in the custom menu bar. I have provided a skeleton GUI to get you started, which you can download here.
-
Now, type
guideat the command prompt.
-
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

-
Here is what the GUI should look like when you open it:

-
Next, go to Tools => Menu Editor as shown in the figure

-
You should see the following window appear. Click on the upper left icon to create a new menu.

-
Now, you can change the Label and Tag properties. I recommend you use the same names that I used in the image below.

-
Next, we need to add menu items. Click on the second icon twice to create two new menu items.

-
Let’s designate the first menu item as the option that will increase the font. Fill out the Label, Tag, and Accelerator accordingly. Note: The accelerator is basically used as a hotkey within the GUI. This means you can press Control + Q to execute the Callback associated with this menu item.

-
Let’s do the same for the second item and make it the option that will decrease the font. After you’re done with this, press the OK button

-
Next, we need to add some code to the callbacks, which were automatically generated for us when we configured the menu items. Go to the m-file and you should see three callbacks that were added. We don’t need any code for the fontMenu_Callback.
For the increaseFont_Callback, we want to add the following code:
currentFontSize = get(handles.blinkdagger,'FontSize'); set(handles.blinkdagger,'FontSize',currentFontSize+2);
Next, we want to add code to the decreaseFont_Callback:
currentFontSize = get(handles.blinkdagger,'FontSize'); set(handles.blinkdagger,'FontSize',currentFontSize-2);
-
Now we are ready to run the GUI. You should see the following GUI appear

-
Notice the menu that you just configured! You can either manually click on the menu items to increase/decrease the font size, or use the accelerator to increase (control + Q) and decease (control + W) the font size. Go ahead and give it a try!

Default + Custom Menu Bar
If you enable the default menu bar and also define a custom menu bar, the custom menu bar will show up at the end of the default menu bar. See the image below.

Download the Source Files
You can download the source files here.
This is the end of the tutorial.
14 Responses to “Matlab GUI - Adding a Custom Menu Bar”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Hi,
Thanks a lot :), i was in search of this tutorial, i never knew how to include menus in my GUI. Man u r genius. It’s cool. Hey can u help me out, i am developing a GUI for calculator (exact the same which is there in the windows xp). How to put those dots subtitles of menu(dots-states that it’s the active selection). I don’t know. can u help me out.
x
It was very useful
glad to be of help!
Thanks Quan….these things aren’t tough to do, but hard to figure out by yourself by reading only the help files and all. Will be keeping an eye on your posts in the future.
Thanks a lot.I was interested in knowing how to add custom menu bar.
[...] the menu on the upper left of the GUI. If you don’t know how to create a custom menu, I suggest you visit this tutorial first. Anyhow, we can go back to the m-file and add in some code for the menu item callbacks. I prefer to [...]
thanks for this tutorial. it is very useful.
Hi,
Is there a way to add “programatically” elements to the menu. For example I have an application developed with GUIDE but at the initial phase the menu reads:
Edit | Project file | Full data save
Edit | Project file | Custom data save
Evidently for each option I have a callback, one for “Full data save” and one for “Custom data save” (I do not mention here the Edit callback and Project File callback because they do not have something to do). But wen I click (check) “Custom data save” I like to see a list of options that I could add also in the menu and the content of the list is changing over time (during development phase). For example at one point I will have
Edit | Project file | Custom data save | dataA
Edit | Project file | Custom data save | dataB
The user can check the option dataA and un-check dataB, i.e, only dataA will be added to the project file. But if later I add 10 more options I do not want to add 10 more items using the GUIDE and I will like to make it automatically, basically add the callbacks for these elements in the main GUI function. This will mean some king of “self written code” procedure that will have to modify my source files. Not so safe and practical I think.
The problem is similar also if you need to implement a “most recent used” files or “last 10 opened projects” with a Matlab GUI.
Is there another option for this?
Nice blog and useful. I found this today and I think I will come more often around! I have many things that I want to challenge you guys. It seams that you like Matlab as much as I do.
Thank you for the work. Regards,
Catalin
Hi,
ı want to use the menu bar for entering the inputs from there, is it possible ı tried to create a edittext in there. but it’s not working.
Can you help me please?
Hi, could u help me? i am trying to put save in my menu bar. but i can’t save my image in the menu bar, i tried ur save code, but it cannot work because it was more for saving the GUI settings.
Hello. If I were to name the label: Reset, how would I enter the coding for it to reset the entire GUI? Thank you!
excelent!
How do I make the menu items bold and bigger; i.e. change the text fonts?
Thanks.