我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

uimenu

Create menus and menu items on figure windows

Syntax

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

Description

m = uimenu creates a uimenu in an existing figure's menu bar and returns the uimenu object, m. If there is no figure available, then MATLAB® creates a new figure to serve as the parent.

m = uimenu(Name,Value,...) creates a uimenu and specifies one or more uimenu property names and corresponding values. Use this syntax to override the default uimenu properties.

m = uimenu(parent) creates a uimenu and designates a specific parent object. The parent argument can be a figure, uicontextmenu, or another uimenu object. Setting the parent to a uicontextmenu or another uimenu makes m a submenu of the parent menu.

m = uimenu(parent,Name,Value,...) creates a uimenu with a specific parent and one or more uimenu properties.

Examples

This example creates a menu labeled Workspace with menu options for creating a new figure window, saving workspace variables, and exiting MATLAB. In addition, it defines an accelerator key for the Quit option.

f = uimenu('Label','Workspace');
    uimenu(f,'Label','New Figure','Callback','disp(''figure'')');
    uimenu(f,'Label','Save','Callback','disp(''save'')');
    uimenu(f,'Label','Quit','Callback','disp(''exit'')',... 
           'Separator','on','Accelerator','Q');

This example creates a new figure with a menu bar that excludes the built-in menus. It creates a Find menu with options Find & Replace, and Variable. For the Variable option, it creates a submenu with options of Name and Value.

f = figure('MenuBar','None');
mh = uimenu(f,'Label','Find'); 
frh = uimenu(mh,'Label','Find and Replace ...',...
            'Callback','disp(''goto'')');
frh = uimenu(mh,'Label','Variable');                 
uimenu(frh,'Label','Name...', ...
          'Callback','disp(''variable'')');

uimenu(frh,'Label','Value...', ...
          'Callback','disp(''value'')');

This example creates a context menu, Font, on a figure with menu options Helvetica and Monospace. When you run the code and then right-click anywhere within the figure window, the context menu displays.

f = figure;
% Create the UICONTEXTMENU
cmenu = uicontextmenu;

% Create the parent menu
fontmenu = uimenu(cmenu,'label','Font');

% Create the submenus
font1 = uimenu(fontmenu,'label','Helvetica',...
               'Callback','disp(''HelvFont'')');
font2 = uimenu(fontmenu,'label',...
               'Monospace','Callback','disp(''MonoFont'')');
f.UIContextMenu = cmenu;

More About

expand all

Tips

If you want the uimenu to respond when the user activates the uimenu, set its Callback property to be a function handle (or a string of MATLAB commands). Uimenus respond to different actions, depending on their location:

  • Top level (in the menu bar) — Responds to a single mouse click

  • Submenu that contains other submenus — Responds to hovering the mouse

  • Terminating submenu — Responds to a single mouse click

The value of the figure's MenuBar property determines the content in the menu bar. When the MenuBar property is set to 'figure', a default set of menus appear alongside the uimenus defined in your GUI code. When the MenuBar property is set to 'none', the default menus do not appear; the uimenus defined in your GUI code appear by themselves.

Uimenus do not appear in figures whose WindowStyle property is set to 'Modal'. If a figure containing uimenu children is changed to 'Modal', the uimenu children still exist in the Children property of the figure. However, the uimenus do not display while WindowStyle is set to 'Modal'.

Was this topic helpful?