uimenu

Create menu or menu items

Syntax

m = uimenu
m = uimenu(Name,Value)
m = uimenu(parent)
m = uimenu(parent,Name,Value)

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');

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
f = uifigure;
m = uimenu(f,'Text','&Import');
 
mitem = uimenu(m,'Text','&Text File');
mitem.Accelerator = 'T';
mitem.MenuSelectedFcn = @MenuSelected;
 
    function MenuSelected(src,event)
        file = uigetfile('*.txt');
    end
 
end

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
f = uifigure;
ax = uiaxes(f);
grid(ax);
btn = uibutton(f,'Text','Show Grid');
btn.Position = [155 325 100 20];

m = uimenu(f,'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

Input Arguments

collapse all

Parent container, specified as a Figure object created with either the figure or uifigure function, or another Menu 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.

To add menu items to a context menu in GUIDE, or context menus in a figure created with the figure function, specify the parent as a ContextMenu object.

Name-Value Pair 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'.

  • For a list of properties available for App Designer, or for creating apps with the uifigure function, see Menu Properties for App Designer.

  • For a list of properties available for GUIDE, or for creating apps with the figure function, see Menu Properties for GUIDE.

Introduced before R2006a