Menu Properties

Control appearance and behavior of menu

The properties listed here are valid for menus in GUIDE or in apps created with the figure function. If you are using App Designer or the uifigure function, see Menu Properties instead. For more information, see Migrating GUIDE Apps to App Designer.

Menus display drop-down lists of options at the top of an app window. Call the uimenu function to create a menu or add a submenu to an existing menu. Properties control the appearance and behavior of a menu. Use dot notation to refer to a specific object and property.

f = figure;
m = uimenu(f);
m.Text = 'Open Selection';

Menu

expand all

Menu label, specified as a character vector or string scalar. This property specifies the label that appears on the menu or menu item.

Avoid using these case-sensitive reserved words: "default", "remove", and "factory". If you must use a reserved word, then specify a backslash character before the word. For instance, specify "default" as '\default'.

You can specify a mnemonic keyboard shortcut (Alt+mnemonic) by using the ampersand (&) character in the text for the label. The character that follows the ampersand appears underlined in the menu when Alt is pressed. You can select the menu item by holding down the Alt key and typing the character shown.

To use mnemonics, you must specify a mnemonic for all menus and menu items that you define in the app. If you define mnemonics only for some menus or menu items, pressing the Alt key does not have any effect. Mnemonics cannot be used on menus or menu items within a context menu.

The table shows some examples:

Text ValueMenu Label with Mnemonic Hints
'&Open Selection'

'O&pen Selection'

'&Save && Go'

Keyboard shortcut, specified as a character. Use this property to define a keyboard shortcut for selecting a menu item.

Example: mitem.Accelerator = 'H'

Specifying an accelerator value enables users to select the menu item by pressing a character and another key, instead of using the mouse. The key sequence is platform specific.

  • Windows® systems: Ctrl+accelerator

  • Macintosh systems: Command+accelerator

  • Linux® systems: Ctrl+accelerator

Things to keep in mind when using accelerators:

  • The app window must be in focus when entering the accelerator key sequence.

  • Accelerators cannot be used on top-level menus.

  • Accelerators only work when the menu item meets all these criteria.

    • It does not contain any submenu items.

    • It executes a callback function.

    • It has the Visible property set to 'on'.

If the menu item is nested within a context menu, then the accelerator will only work if the context menu is in focus.

Separator line above menu item, specified as 'off' or 'on'. Setting this property to 'on' draws a dividing line above the menu item.

Note

The Separator property is ignored when the menu item is a top-level menu item.

Menu check indicator, specified as 'off' or 'on'. Setting this property to 'on' places a check mark next to the corresponding menu item. Setting it to 'off' removes the check mark. You can use this feature to show the state of menu items that enable or disable functionality in your application.

Note

The Checked property is ignored when the menu item is:

  • A top-level menu item

  • A menu item that contains one or more child menu items

Menu label color, specified as an RGB triplet, a hexadecimal color code, or one of the color options listed in the table.

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes '#FF8800', '#ff8800', '#F80', and '#f80' are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
'red''r'[1 0 0]'#FF0000'

'green''g'[0 1 0]'#00FF00'

'blue''b'[0 0 1]'#0000FF'

'cyan' 'c'[0 1 1]'#00FFFF'

'magenta''m'[1 0 1]'#FF00FF'

'yellow''y'[1 1 0]'#FFFF00'

'black''k'[0 0 0]'#000000'

'white''w'[1 1 1]'#FFFFFF'

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]'#0072BD'

[0.8500 0.3250 0.0980]'#D95319'

[0.9290 0.6940 0.1250]'#EDB120'

[0.4940 0.1840 0.5560]'#7E2F8E'

[0.4660 0.6740 0.1880]'#77AC30'

[0.3010 0.7450 0.9330]'#4DBEEE'

[0.6350 0.0780 0.1840]'#A2142F'

Menu label, specified as a character vector or string scalar. This property specifies the label that appears on the menu (or menu item).

Note

Use of the Label property is not recommended. It might be removed in a future release. Use the Text property instead.

Relative menu position, specified as a scalar integer value. The value of Position property indicates placement on the menu bar or within a menu. Top-level menus appear from left to right on the menu bar according to the value of their Position property, with 1 representing the left-most position. The individual items within a given menu appear from top to bottom according to the value of their Position property, with 1 representing the top-most position.

Note

Use of the Position property is not recommended. It might be removed in a future release. To change the order of menu items, use the Children property or the uistack function instead.

Interactivity

expand all

Component visibility, specified as 'on' or 'off'. When the Visible property is set to 'off', the component is not visible in the UI, but you can query and set its properties.

Operational state of menu, specified as 'on' or 'off'. This property controls whether the user can select a menu item. When the value is 'off', the menu label appears dimmed, indicating that the user cannot select it.

Callbacks

expand all

Menu selected callback, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

For more information about specifying a callback property value as a function handle, cell array, or character vector, see How to Specify Callback Property Values.

This callback responds depending on the location of the menu item and the type of interaction:

  • Left-clicking a menu expands that menu and triggers its callback.

  • While any menu is expanded, hovering any other parent menu (or top-level menu) expands that menu and triggers its callback.

Note

Do not use a callback to dynamically change menu items. Deleting, adding, and replacing menu items in a callback can result in a blank menu. Instead, use the Visible property to hide or show menu items. You can also enable and disable menu items by setting the Enable property. To fully repopulate menu items, delete and create them outside the callback.

Component creation function, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

For more information about specifying a callback property value as a function handle, cell array, or character vector, see How to Specify Callback Property Values.

This property specifies a callback function to execute when MATLAB creates the component. MATLAB initializes all component property values before executing the CreateFcn callback. If you do not specify the CreateFcn property, then MATLAB executes a default creation function.

Use the gcbo function in your CreateFcn code to get the component object that is being created.

Setting the CreateFcn property on an existing component object has no effect.

Component deletion function, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

For more information about specifying a callback property value as a function handle, cell array, or character vector, see How to Specify Callback Property Values.

The DeleteFcn property specifies a callback function to execute when MATLAB deletes the component (for example, when the user closes the window). MATLAB executes the DeleteFcn callback before destroying the properties of the component object. If you do not specify the DeleteFcn property, then MATLAB executes a default deletion function.

Use the gcbo function in your DeleteFcn code to get the component object that is being deleted.

Menu selected callback, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

Note

Use of the Callback property is not recommended. It might be removed in a future release. Use MenuSelectedFcn instead.

Callback Execution Control

expand all

Callback interruption, specified as 'on' or 'off'. The Interruptible property determines if a running callback can be interrupted.

There are two callback states to consider:

  • The running callback is the currently executing callback.

  • The interrupting callback is a callback that tries to interrupt the running callback.

Whenever MATLAB invokes a callback, that callback attempts to interrupt the running callback (if one exists). The Interruptible property of the object owning the running callback determines if interruption is allowed:

  • A value of 'on' allows other callbacks to interrupt the object's callbacks. The interruption occurs at the next point where MATLAB processes the queue, such as when there is a drawnow, figure, getframe, waitfor, or pause.

    • If the running callback contains one of these commands, then MATLAB stops the execution of the callback at this point and executes the interrupting callback. MATLAB resumes executing the running callback when the interrupting callback completes.

    • If the running callback does not contain one of these commands, then MATLAB finishes executing the callback without interruption.

  • A value of 'off' blocks all interruption attempts. The BusyAction property of the object owning the interrupting callback determines if the interrupting callback is discarded or put into a queue.

Note

Callback interruption and execution behave differently in these situations:

  • If the interrupting callback is a DeleteFcn, CloseRequestFcn, or SizeChangedFcn callback, then the interruption occurs regardless of the Interruptible property value.

  • If the running callback is currently executing the waitfor function, then the interruption occurs regardless of the Interruptible property value.

  • Timer objects execute according to schedule regardless of the Interruptible property value.

  • MATLAB does not save the state of properties or the display when an interruption occurs. For example, the object returned by the gca or gcf command might change when another callback executes.

See Interrupt Callback Execution for an example that shows how the Interruptible and BusyAction properties affect the behavior of a program.

Callback queuing specified as 'queue' (default) or 'cancel'. The BusyAction property determines how MATLAB handles the execution of interrupting callbacks. There are two callback states to consider:

  • The running callback is the currently executing callback.

  • The interrupting callback is a callback that tries to interrupt the running callback.

The BusyAction property of the source of the interrupting callback determines how MATLAB handles its execution. The BusyAction property has these values:

  • 'queue' — Put the interrupting callback in a queue to be processed after the running callback finishes execution.

  • 'cancel' — Do not execute the interrupting callback.

Whenever MATLAB invokes a callback, that callback always attempts to interrupt an executing callback. The Interruptible property of the object whose callback is running determines if interruption is allowed. If Interruptible is set to:

  • on — Interruption occurs at the next point where MATLAB processes the queue. This is the default.

  • off — The BusyAction property (of the object owning the interrupting callback) determines if MATLAB enqueues or ignores the interrupting callback.

See Interrupt Callback Execution for an example that shows how the BusyAction and Interruptible properties affect the behavior of a program.

This property is read-only.

Deletion status, returned as 'off' or 'on'. MATLAB sets the BeingDeleted property to 'on' when the DeleteFcn callback begins execution. The BeingDeleted property remains set to 'on' until the component object no longer exists.

Check the value of the BeingDeleted property to verify that the object is not about to be deleted before querying or modifying it.

This property has no effect on Menu objects.

Parent/Child

expand all

This property is read-only.

Parent object, specified as a Figure, ContextMenu, or a Menu object. You can move a menu item to a different window, or move it under a different menu by setting this property.

Menu children, returned as an empty GraphicsPlaceholder or a 1-D array of Menu objects. The children of menus are other menus that function as submenus.

You cannot add or remove children using the Children property. Use this property to view the list of children or to reorder the child menu items. The order of the children in this array reflects the order of the displayed menu items.

To add a child to this list, set the Parent property of the child component to be the Menu object.

Objects with the HandleVisibility property set to 'off' do not list in the Children property.

Visibility of object handle, specified as 'on', 'callback', or 'off'.

This property controls the visibility of the object handle in its parent's list of children. When a handle is not visible in its parent's list of children, it is not returned by functions that obtain handles by searching the object hierarchy or querying handle properties. These functions include get, findobj, gca, gcf, gco, newplot, cla, clf, and close. The HandleVisibility property also controls the visibility of the object’s handle in the parent figure's CurrentObject property. Handles are still valid even if they are not visible. If you can access an object, you can set and get its properties, and pass it to any function that operates on objects.

HandleVisibility ValueDescription
'on'The object handle is always visible.
'callback'The object handle is visible from within callbacks or functions invoked by callbacks, but not from within functions invoked from the command line. This option blocks access to the object at the command-line, but allows callback functions to access it.
'off'The object handle is invisible at all times. This option is useful for preventing unintended changes to the UI by another function. Set the HandleVisibility to 'off' to temporarily hide the handle during the execution of that function.

Set the graphics root ShowHiddenHandles property to 'on' to make all handles visible, regardless of their HandleVisibility value. This setting has no effect on their HandleVisibility values.

Identifiers

expand all

This property is read-only.

Type of graphics object, returned as 'uimenu'.

Object identifier, specified as a character vector or string scalar. You can specify a unique Tag value to serve as an identifier for an object. When you need access to the object elsewhere in your code, you can use the findobj function to search for the object based on the Tag value.

User data, specified as any array. Specifying UserData can be useful for sharing data within apps. See Share Data Among Callbacks for more information.

Introduced before R2006a