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

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 comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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'

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

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.

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

For more information about writing callbacks, see Write Callbacks in App Designer.

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

Introduced before R2006a