Introduction

Matlab Logo 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:

Default Menubar

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.

  1. Add the following line of code to the opening function:

    set(handles.figure1,'MenuBar','figure');
  2. Double click on the background of the GUIDE figure to bring up the Property Inspector. Set the MenuBar property to “figure”.

    Property Inspector

Adding your own Custom Menu Bar: A Simple Example

  1. 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.

  2. Now, type guide at the command prompt.

    Command prompt

  3. Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

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

    GUI Figure

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

    Select Menu Editor

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

    Menu Editor Window

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

    Change Menu Name and Tag

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

    Add Menu Items

  9. 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.

    Modify Menu Item

  10. 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

    Modify Menu Item

  11. 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);
  12. Now we are ready to run the GUI. You should see the following GUI appear

    Run GUI

  13. 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!

    Test the GUI

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.

Default + Custom Menubar

Download the Source Files

You can download the source files here.

This is the end of the tutorial.