Main Content

uimenu

Create menu or menu items

Description

m = uimenu creates a menu in the current figure and returns the Menu object. If there is no figure available, MATLAB® calls the figure function to create one.

example

m = uimenu(Name,Value) specifies menu property values using one or more name-value pair arguments.

m = uimenu(parent) creates the menu in the specified parent container. The parent container can be a figure created with either the figure or uifigure function, or another Menu object. Property values for uimenu vary slightly depending on whether the app is created with the figure or uifigure function. For more information, see Name-Value Arguments.

example

m = uimenu(parent,Name,Value) specifies the parent container and one or more property values.

Examples

collapse all

Create a figure that displays the default menu bar. Add a menu and a menu item.

f = figure('Toolbar','none');
m = uimenu('Text','Options');
mitem = uimenu(m,'Text','Reset');

A figure window with a menu bar. The menu items are "File", "Edit", "View", "Insert", "Tools", "Desktop", "Window", "Help", and "Options". The "Options" item is selected, and displays a drop-down with a "Reset" option.

Add a menu item with keyboard shortcuts to the menu bar and define a callback that executes when the menu item is selected.

First, create a program file called importmenu.m. Within the program file:

  • Create a figure.

  • Add a menu called Import. Create a mnemonic keyboard shortcut for the menu by specifying '&Import' as the text label.

  • Create a menu item and specify mnemonic and accelerator keyboard shortcuts.

  • Define a MenuSelectedFcn callback that executes when the user clicks the menu item or uses the mnemonic or accelerator keyboard shortcuts.

Run the program file.

function importmenu
fig = uifigure;
m = uimenu(fig,'Text','&Import');
 
mitem = uimenu(m,'Text','&Text File');
mitem.Accelerator = 'T';
mitem.MenuSelectedFcn = @MenuSelected;
 
    function MenuSelected(src,event)
        file = uigetfile('*.txt');
    end
 
end

A menu bar with an "Import" item with a "Text File" sub-item. The "I" in "Import" and the "T" in "Text File" are underlined. The Ctrl+T keyboard shortcut is displayed to the right of the "Text File" item.

You can interact with the menu and menu item, using the keyboard, in the following ways:

  • Select the Import menu by pressing Alt+I.

  • Select the Text File menu item and execute the callback by pressing Alt+I+T.

  • Select the Text File menu item and execute the callback by using the accelerator Ctrl+T.

When you select the Text File menu item, the Select File to Open dialog box opens with the extension field filtered to text files.

File dialog box. The file extension filer drop-down list has the option "(*.txt.)" selected.

Create a checked menu item that can be selected or cleared to show a grid in axes. Share the callback with a push button so that pushing it also shows or hides the grid.

First, create a program file called plotOptions.m. Within the program file:

  • Create a figure with a push button, and axes that display a grid.

  • Add a menu and a menu item with mnemonics. Specify that the menu item is checked.

  • Define a MenuSelectedFcn callback that hides or shows the grid when the user interacts with the menu item.

  • Define a ButtonPushedFcn that uses the same callback function as the menu item.

Run the program file.

function plotOptions
fig = uifigure;
ax = uiaxes(fig);
grid(ax);
btn = uibutton(fig,'Text','Show Grid');
btn.Position = [155 325 100 20];

m = uimenu(fig,'Text','&Plot Options');
mitem = uimenu(m,'Text','Show &Grid','Checked','on');
mitem.MenuSelectedFcn = @ShowGrid;
btn.ButtonPushedFcn = @ShowGrid;

    function ShowGrid(src,event)
        grid(ax);
        if strcmp(mitem.Checked,'on')
            mitem.Checked = 'off';
        else
            mitem.Checked = 'on';
        end
    end
end

An app with a menu bar, button, and set of axes. The "Show Grid" menu item has a checked check box to the left of the text.

Input Arguments

collapse all

Parent container, specified as a Figure object created with either the figure or uifigure function, another Menu object, or a ContextMenu object. If you do not specify a parent container, then MATLAB calls figure to create one, and places the menu in the menu bar of that figure. Specify the parent as an existing Menu object to add menu items to a menu, or to nest menu items.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: m = uimenu('Text','Open') creates a menu and sets its label to 'Open'.

Note

The properties listed here are a subset of the available properties. For the full list, see Menu Properties.

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.

The table shows some examples:

Text ValueMenu Label with Mnemonic Hints
'&Open Selection'

Open Selection menu label. The "O" in "Open" is underlined.

'O&pen Selection'

Open Selection menu label. The "p" in "Open" is underlined.

'&Save && Go'

Open Selection menu label. The "S" in "Save & Go" is underlined.

Keyboard shortcut, specified as a character or as a string that contains one 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'.

    • Its accelerator value is not already assigned to a different active menu item in the same app.

Menu selected callback 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 Specify a Callback Function.

The 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, pausing on 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.

Menus Associated with Context Menus

When the menu component is associated with a context menu (as opposed to a menu item at the top of a figure window), this callback function can access specific information about the user's interaction with the app. MATLAB passes this information in a MenuSelectedData object as the second argument to your callback function. In App Designer, the argument is named event. You can query the object properties using dot notation. For example, event.ContextObject returns information about which component the user right-clicked to open the associated context menu.

Note

You can specify a MenuSelectedFcn callback for any Menu object. However, the MenuSelectedData object in the callback event data is available only when the context menu that the menu belongs to satisfies both of these conditions:

  • The context menu is associated with a uifigure-based app (such as an app created in App Designer).

  • The context menu is associated with a UI component (as opposed to a graphics object, such as an Axes or Line object).

This table lists the properties of the MenuSelectedData object.

PropertyValue
ContextObjectObject that the app user right-clicked to open the context menu
InteractionInformation

Information about where in the component the app user right-clicked to open the context menu. This information is stored as an object with different properties depending on the value of ContextObject.

For example, if ContextObject is a Table object, then InteractionInformation stores information about which row and column in the table the user right-clicked. For more details on the properties that InteractionInformation can have, see the next table.

SourceContext menu object that executes the callback
EventName'MenuSelected'

This table lists the properties of the InteractionInformation object. The properties depend on which object the app user right-clicked to open the context menu.

ContextObjectInteractionInformation PropertyValue
AnyLocation

Location where the user right-clicked relative to the bottom-left corner of the parent container of the ContextObject, returned as a two-element vector of the form [x y].

The value of x represents the horizontal distance from the left edge of the parent container to the right-click location. The value of y represents the vertical distance from the bottom edge of the parent container to the right-click location. Distances are measured in pixels.

ScreenLocation

Location where the user right-clicked relative to the bottom-left corner of their primary display, returned as a two-element vector of the form [x y].

The value of x represents the horizontal distance from the left edge of the display to the right-click location. The value of y represents the vertical distance from the bottom edge of the display to the right-click location. Distances are measured in pixels.

TableDisplayRow

Row that the user right-clicked as it appears visually in the table, returned as a numeric scalar.

If the user has not sorted the table, then DisplayRow has the same value as Row. If the user right-clicked an area of the table UI component that is not associated with a row, then DisplayRow is an empty array.

DisplayColumn

Column that the user right-clicked as it appears visually in the table, returned as a numeric scalar.

If the user has not rearranged the table, then DisplayColumn has the same value as Column. If the user right-clicked an area of the table UI component that is not associated with a column, then DisplayColumn is an empty array.

Row

Row that the user right-clicked as it corresponds to the original table data, returned as a numeric scalar.

If the user has not sorted the table, then Row has the same value as DisplayRow. If the user right-clicked an area of the table UI component that is not associated with a row, then Row is an empty array.

Column

Column that the user right-clicked as it corresponds to the original table data, returned as a numeric scalar.

If the user has not rearranged the table, then Column has the same value as DisplayColumn. If the user right-clicked an area of the table UI component that is not associated with a column, then Column is an empty array.

RowHeaderWhether the user right-clicked the table row header, returned as a logical 0 (false) or 1 (true).
ColumnHeaderWhether the user right-clicked the table column header, returned as a logical 0 (false) or 1 (true).
TreeNode

Right-clicked node, returned as a TreeNode object.

If the user right-clicked an area of the tree that is not associated with a node, then Node is an empty array.

Level

Level of the right-clicked node, returned as a numeric scalar. Nodes parented directly to the Tree object are at level 1, nodes parented to a node at level 1 are at level 2, and so on.

If the user right-clicked an area of the tree that is not associated with a node, then Level is an empty array.

ListBoxItem

Index of the right-clicked list box item, returned as a numeric scalar.

If the user right-clicked an area of the list box that is not associated with an item, then Item is an empty array.

Separator line mode, specified as 'off' or 'on', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

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', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

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

Version History

Introduced before R2006a