主要内容

uimenu

创建菜单或菜单项

描述

m = uimenu在当前图中创建菜单,然后返回菜单目的。如果没有数字,MATLAB®打电话figure函数创建一个。

example

m = uimenu(名称,价值)specifies menu property values using one or more name-value pair arguments.

m = uimenu()在指定的父容器中创建菜单。父容器可以是用任何一种创建的数字figureoruifigure功能,或另一个菜单目的。Property values foruimenu略有不同,取决于是否与该应用程序创建figureoruifigure功能。For more information, see名称值对参数

example

m = uimenu(,名称,价值)specifies the parent container and one or more property values.

例子

全部收缩

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

f = figure(“工具栏”,'none');m = uimenu('Text','选项');mitem = uimenu(m,'Text','重置');

A figure window with a menu bar. The menu items are

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 calledimportmenu.m。Within the program file:

  • Create a figure.

  • Add a menu called进口。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菜单螺旋callback that executes when the user clicks the menu item or uses the mnemonic or accelerator keyboard shortcuts.

运行程序文件。

功能importMenu fig = uifigure;m = uimenu(图,'Text','&Import');mitem = uimenu(m,'Text','&Text File');mitem.Accelerator ='T';mitem.menuselectedfcn = @menuselected;功能菜单选择(src,event)file = uigetfile('*.txt');endend

带有“导入”项目的菜单栏,带有“文本文件”子项目。强调了“导入”中的“ i”和“文本文件”中的“ t”。CTRL+T键盘快捷键显示在“文本文件”项目的右侧。

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

  • 选择进口menu by pressingalt+i

  • 选择文本File菜单项并按下执行回调alt+i+t

  • 选择文本Filemenu item and execute the callback by using the acceleratorctrl+t

When you select the文本File菜单项,“选择要打开对话框”的文件打开,并使用已过滤到文本文件的扩展字段。

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 calledplotoptions.m。Within the program file:

  • 用按钮创建一个图形,并显示显示网格的轴。

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

  • Define a菜单螺旋回调,隐藏或显示了网格时使用r interacts with the menu item.

  • Define abuttonpushedfcnthat uses the same callback function as the menu item.

运行程序文件。

功能plotOptions fig = uifigure; ax = uiaxes(fig); grid(ax); btn = uibutton(fig,'Text','Show Grid');btn.position = [155 325 100 20];m = uimenu(图,'Text','&Plot Options');mitem = uimenu(m,'Text','Show &Grid',“检查”,'on');mitem.menuselectedfcn = @showgrid;btn.buttonpushedfcn = @showgrid;功能ShowGrid(src,event) grid(ax);如果strcmp(Mitem.Chacked,'on') mitem.Checked ='离开';别的mitem.Checked ='on';endendend

An app with a menu bar, button, and set of axes. The

输入参数

全部收缩

Parent container, specified as aFigureobject created with either thefigureoruifigure功能,另一个菜单object, or aContextMenu目的。如果您不指定父容器,则将MATLAB调用figure创建一个,然后将菜单放在该图的菜单栏中。将父级指定为现有菜单object to add menu items to a menu, or to nest menu items.

Name-Value Arguments

将可选的参数对Name1=Value1,...,NameN=ValueN, 在哪里Nameis the argument name and价值是相应的值。名称值参数必须在其他参数之后出现,但是对的顺序并不重要。

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

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

Note

此处列出的属性是可用属性的子集。有关完整列表,请参阅菜单Properties

菜单标签,指定为字符向量或字符串标量。此属性指定出现在菜单或菜单项上的标签。

避免使用这些对案例敏感的保留词:'default','remove', 和'工厂'。If you must use a reserved word, then specify a backslash character before the word. For instance, specify'default'as'\default'

您可以指定助记符键盘快捷键(alt+mnemonic) by using the ampersand (&) character in the text for the label. The character that follows the ampersand appears underlined in the menu whenalt被按下。您可以通过按住altkey and typing the character shown.

要使用Mnemonics,您必须为您在应用程序中定义的所有菜单和菜单项指定助记符。如果仅针对某些菜单或菜单项定义助记符,请按altkey does not have any effect.

The table shows some examples:

文本价值 菜单Label with Mnemonic Hints
'&Open Selection'

'O&pen选择'

'&save && go'

键盘快捷键,指定为字符或包含一个字符的字符串。使用此属性定义用于选择菜单项的键盘快捷键。

例子:mitem.accelerator =“ h”

指定加速器值使用户可以通过按字符和另一个键来选择菜单项,而不是使用鼠标。关键序列是特定于平台的。

  • 视窗®系统:Ctrl+加速器

  • Macintosh系统:命令+加速器

  • Linux®系统:Ctrl+加速器

Things to keep in mind when using accelerators:

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

  • 加速器不能用于顶级菜单。

  • 加速器仅在菜单项符合所有这些条件时才起作用。

    • It does not contain any submenu items.

    • It executes a callback function.

    • 它有可见的property set to'on'

菜单选择的回调,指定为以下值之一:

  • 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:

  • 左键单击菜单会扩展该菜单并触发其回调。

  • 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可见的隐藏或显示菜单项的属性。您还可以通过设置Enable财产。要完全重新填充菜单项,请删除并在回调之外创建它们。

For more information about writing callbacks, see应用程序设计师中的回调

分离器线模式,指定为'离开'or'on',或数字或逻辑1(真的) 或者0(false)。一个值'on'相当于真的, 和'离开'相当于false。因此,您可以将此属性的值用作逻辑值。该值存储为类型的ON/OFF逻辑值matlab.lang.onoffswitchstate

将此属性设置为'on'draws a dividing line above the menu item.

Note

TheSeparator当菜单项是顶级菜单项时,将忽略属性。

菜单check indicator, specified as'离开'or'on',或数字或逻辑1(真的) 或者0(false)。一个值'on'相当于真的, 和'离开'相当于false。因此,您可以将此属性的值用作逻辑值。该值存储为类型的ON/OFF逻辑值matlab.lang.onoffswitchstate

将此属性设置为'on'places a check mark next to the corresponding menu item. Setting it to'离开'删除复选标记。您可以使用此功能显示启用或禁用应用程序功能的菜单项的状态。

Note

TheCheckedproperty is ignored when the menu item is:

  • A top-level menu item

  • 包含一个或多个子菜单项的菜单项

Version History

在R2006a之前引入