Creating Menus

You can use GUIDE to give GUIs menu bars with pull-down menus as well as context menus that you attach to components. You can create both types of menus using the Menu Editor. Access the Menu Editor from the Tools menu or click the Menu Editor button .

menu editor

Menus for the Menu Bar

When you create a drop-down menu, GUIDE adds its title to the GUI menu bar. You then can create menu items for that menu. Each menu item can have a cascading menu, also known as a submenu, and these items can have cascading menus, and so on.

How Menus Affect Figure Docking

By default, when you create a GUI with GUIDE, it does not create a menu bar for that GUI. You might not need menus for your GUI, but if you want the user to be able to dock or undock the GUI, it must contain a menu bar or a toolbar. This is because docking is controlled by the docking icon, a small curved arrow near the upper-right corner of the menu bar or the toolbar, as the following illustration shows.

Figure windows with a standard menu bar also have a Desktop menu from which the user can dock and undock them.

To display the docking arrow and the Desktop > Dock Figure menu item, use the Property Inspector to set the figure property DockControls to 'on'. You must also set the MenuBar and/or ToolBar figure properties to 'on' to display docking controls.

The WindowStyle figure property also affects docking behavior. The default is 'normal', but if you change it to 'docked', then the following applies:

However, when you provide your own menu bar or toolbar using GUIDE, it can display the docking arrow if you want the GUI to be dockable. See the following sections and Creating Toolbars for details.

For more information, see the DockControls, MenuBar, ToolBar, and WindowStyle property descriptions on the figure properties reference page, or select the figure background in GUIDE right-click these property names in the Property Inspector.

Adding Standard Menus to the Menu Bar

The figure MenuBar property controls whether your GUI displays the MATLAB standard menus on the menu bar. GUIDE initially sets the value of MenuBar to none. If you want your GUI to display the MATLAB standard menus, use the Property Inspector to set MenuBar to figure.

In either case, you can enable users of your GUI to dock and undock it using its docking arrow by setting the figure's DockControls property to 'on'.

Creating a Menu

  1. Start a new menu by clicking the New Menu button in the toolbar. A menu title, Untitled 1, appears in the left pane of the dialog box.

    menu editor with cursor pointing to the new menu  button

  2. Click the menu title to display a selection of menu properties in the right pane.

    menu editor showing file properties

  3. Fill in the Label and Tag fields for the menu. For example, set Label to File and set Tag to file_menu. Click outside the field for the change to take effect.

    Label is a string that specifies the text label for the menu item. To display the & character in a label, use two & characters in the string. The words remove, default, and factory (case sensitive) are reserved. To use one of these as labels, prepend a backslash (\) to the string. For example, \remove yields remove.

    Tag is a string that is an identifier for the menu object. It is used in the code to identify the menu item and must be unique in the GUI.

Adding Items to a Menu

Use the New Menu Item tool to create menu items that are displayed in the drop-down menu.

  1. Add an Open menu item under File, by selecting File then clicking the New Menu Item button in the toolbar. A temporary numbered menu item label, Untitled, appears.

    menu editor with cursor pointing to new menu item

  2. Fill in the Label and Tag fields for the new menu item. For example, set Label to Open and set Tag to menu_file_open. Click outside the field for the change to take effect.

    menu editor with entries in the label and tag fields

You can also

Additional Drop-Down Menus

To create additional drop-down menus, use the New Menu button in the same way you did to create the File menu. For example, the following figure also shows an Edit drop-down menu.

Cascading Menus

To create a cascading menu, select the menu item that will be the title for the cascading menu, then click the New Menu Item button. In the example below, Copy is a cascading menu.

menu editor used to create a cascading menu

The following Menu Editor illustration shows three menus defined for the figure menu bar.

menu editor showing three menu types

When you run the GUI, the menu titles appear in the menu bar.

GUI with menu titles shown in menu bar

Context Menus

A context menu is displayed when a user right-clicks the object for which the menu is defined. The Menu Editor enables you to define context menus and associate them with objects in the layout. The process has three steps:

  1. Creating the Parent Menu

  2. Adding Items to the Context Menu

  3. Associating the Context Menu with an Object

Creating the Parent Menu

All items in a context menu are children of a menu that is not displayed on the figure menu bar. To define the parent menu:

  1. Select the Menu Editor's Context Menus tab and select the New Context Menu button from the toolbar.

    menu editor with cursor pointing to new context menu

  2. Select the menu and specify the Tag field to identify the context menu (axes_context_menu in this example).

Adding Items to the Context Menu

Use the New Menu Item button to create menu items that are displayed in the context menu.

  1. Add a Blue background color menu item to the menu by selecting axes_context_menu and clicking the New Menu Item tool. A temporary numbered menu item label, Untitled, appears.

  2. Fill in the Label and Tag fields for the new menu item. For example, set Label to Blue background color and set Tag to blue_background. Click outside the field for the change to take effect.

You can also

Associating the Context Menu with an Object

  1. In the Layout Editor, select the object for which you are defining the context menu.

  2. Use the Property Inspector to set this object's UIContextMenu property to the name of the desired context menu.

The following figure shows the UIContextMenu property for the axes object with Tag property axes1.

property inspector menu

In the GUI M-file, complete the callback subfunction for each item in the context menu. Each callback executes when a user selects the associated context menu item. See Menu Item for information on defining the syntax.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS