Creating Graphical User Interfaces |
Defining Context Menus
A context menu is displayed when a user right-clicks on 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.
See Defining Menus for the Menu Bar for information about defining menus in general.
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, select New Context Menu from the Menu Editor's toolbar.
Select the menu and specify the Tag
to identify the context menu (axes_context_menu
in this example).
Adding Items to the Context Menu
Create the items that will appear in the context menu using New Menu Item on the Menu Editor's toolbar.
When you select the menu item, the Menu Editor displays fields for you to enter the Label
and Tag
properties of the menu item.
A check is particularly useful to indicate the current state of the menu item. For example, suppose you have a menu item called Show axes that toggles the visibility of an axes between visible and invisible each time the user selects the menu item. If you want a check to appear next to the menu item when the axes are visible, add the following code to the callback for the Show axes menu item:
if strcmp(get(gcbo, 'Checked'),'on') set(gcbo, 'Checked', 'off'); else set(gcbo, 'Checked', 'on'); end
This changes the value of the Checked
property of the menu item from on
to off
or vice versa each time a user selects the menu item.
If you set the axes to be visible when a user first opens the GUI, make sure to select the Check mark this item check box in the Menu Editor, so that a check will appear next to the Show axes menu item initially.
Associating the Context Menu with an Object
In the Layout Editor, select the object for which you are defining the context menu. Use the Property Inspector to set this object's UIContextMenu
property to the name of the desired context 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 Callbacks for information on defining the syntax.
Menu Callbacks | Setting the Tab Order |
© 1994-2005 The MathWorks, Inc.