主要内容

Uicontextmenu

Create context menu component

描述

cm= uicontextmenucreates a context menu in the current figure and returns theContextMenu目的。If a figure does not exist, then MATLAB®打电话figure函数创建一个。

To enable the context menu to open in the figure, you must also take these steps:

  • 将上下文菜单分配给同一图中的UI组件或图形对象。

  • 创建一个t least one child菜单上下文菜单中的对象。

example

cm= uicontextmenu()在指定的父数图中创建上下文菜单。父母可以是用任何一种创建的数字uifigure或者figure功能。

example

cm= uicontextmenu(___,名称,价值)创建一个使用一个或多个名称值参数指定的属性值的上下文菜单。用任何以前的语法指定名称值参数。

例子

全部折叠

创建一个context menu with two menu items in a UI figure. Assign the context menu to the figure itself by setting theContextMenuproperty of the figure to theContextMenu目的。要查看上下文菜单,请右键单击“图”窗口中的任何地方。

无花果= uifigure;cm = uicontextmenu(图);m1 = uimenu(cm,'Text','Menu1');m2 = uimenu(cm,'Text','Menu2');fig.ContextMenu = cm;

上下文菜单带有两个选项:“菜单1”和“菜单2”

创建一个UI figure with a button. Then, create a context menu with two menu items in the figure and assign the context menu to the button. To view the context menu, right-click on the button.

无花果= uifigure;btn = uibutton(图);cm = uicontextmenu(图);m1 = uimenu(cm,“文本”,"Option 1");m2 = uimenu(cm,“文本”,"Option 2");btn.contextmenu = cm;

Context menu for a button with two options:

创建一个context menu for a tree component. Assign the context menu to all of the top-level nodes in the tree.

In a new script in your current folder, create a UI figure. Then, create a tree with four top-level nodes and a set of nested nodes.

无花果= uifigure;t = uitree(图,"Position",[20 200 175 100]);category1 = uitreenode(t,“文本”,“跑步者”);r1 = uitreenode(category1,“文本”,“乔”);r2 = uitreenode(category1,“文本”,“琳达”);category2 = uitreenode(t,“文本”,"Cyclists");c1 = uitreenode(category2,“文本”,"Rajeev");category3 = uitreenode(t,“文本”,"Hikers");h1 = uitreenode(category3,“文本”,“杰克”);category4 = uitreenode(t,“文本”,“游泳者”);s1 = uitreenode (category4,“文本”,"Logan");

Tree with four collapsed top-level nodes

使用一个菜单项创建上下文菜单,用户可以单击以扩展单个树节点或所有树节点。对于每个子菜单,指定一个菜单螺旋回调函数将在用户选择菜单选项时执行。将相关的应用对象作为输入传递到每个函数,以从回调函数中访问应用程序数据。

cm = uicontextmenu(图);m1 = uimenu(cm,“文本”,“扩张...”);sbm1 = uimenu(m1,“文本”,"This Node",..."MenuSelectedFcn",{@ExpandSingle,fig});sbm2 = uimenu(m1,“文本”,“所有节点”,..."MenuSelectedFcn",{@expandAll,t});

通过设置上下文菜单将上下文菜单分配给顶级树节点ContextMenu每个节点的属性ContextMenu目的。

category1.contextmenu = cm;category2.contextmenu = cm;category3.contextmenu = cm;类别4.ContextMenu = cm;

At the bottom of the file, define the展开展开全部callback functions. Define the functions to accept the source and event data that MATLAB passes to all callback functions.

定义展开功能还可以接受包含树的UI图对象,并使用CurrentObjectproperty of the figure to determine which tree node was clicked to bring up the context menu. Then, expand that node.

定义展开全部功能还可以接受树对象,并扩展树中的所有节点。

功能ExpandSingle(src,event,f)node = f.currentObject;扩展(节点)end功能展开全部(src,event,t) expand(t)end

保存并运行脚本。右键单击任何顶级树节点以查看上下文菜单。

与“骑自行车者”节点相关的上下文菜单。突出显示了“展开”菜单选项,并且有一个带有选项“此节点”和“所有节点”的子菜单。

创建一个context menu that prints a message in the Command Window each time you open it.

在传统人物中创建线图。然后,使用一个菜单项创建上下文菜单,并将其分配给行绘图。创建一个contection -menuopeningfcn每次打开上下文菜单时,都会在命令窗口中显示输出的回调函数。

f = figure; p = plot(1:10); cm = uicontextmenu(f); m = uimenu(cm,“文本”,“菜单1”);cm.contextmenuopeningfcn = @(src,event)disp("Context menu opened");P.ContextMenu = CM;

To view the context menu, right-click the plot line. When the context menu opens, the Command Window also displays the message:Context menu opened.

The context menu with a menu item called

输入参数

全部折叠

Parent figure, specified as aFigureobject created with either theuifigure或者figure功能。If a parent figure is not specified, then MATLAB calls thefigure函数创建一个用作父母的功能。

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.

例子:uicontextmenu(“ contextMenuopeningfcn”,@myFunction)指定myfunction是当用户打开上下文菜单时执行的函数。

Note

The properties listed here are only a subset. For a complete list, seeContextMenu Properties.

上下文菜单打开回调函数,指定为以下值之一:

  • 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, seeSpecify a Callback Function.

Context menu children, returned as an empty图形Placeholder或者a vector of菜单对象。

您不能使用孩子们财产。使用此属性查看儿童列表或重新排序子菜单项。此数组中的孩子的顺序反映了菜单项在打开的上下文菜单中出现的方式的反向顺序。

For example, this code creates three context menus. When you open the context menu in the running app,菜单1appears as the first menu option.

无花果= uifigure;cm = uicontextmenu(图);m1 = uimenu(cm,'Text','Menu1');m2 = uimenu(cm,'Text','Menu2');m3 = uimenu(cm,'Text','Menu3');fig.ContextMenu = cm;
上下文菜单,带有三个菜单项。

厘米。孩子们returns a list of the menu items in the reverse order.

厘米。孩子们
ANS = 3×1菜单阵列:菜单(菜单3)菜单(菜单2)菜单(菜单1)

Objects with theHandleVisibilityproperty set to'离开'are not listed in the孩子们财产。

To add a child to this list, set theParentproperty of another菜单对此ContextMenu目的。

提示

  • To display a context menu interactively in a running app, it must:

    • 至少有一个菜单项。

    • Be assigned to a UI component or graphics object in the same figure.

  • 要以编程方式打开上下文菜单,请使用open功能。上下文菜单必须是与uifigure功能。To display the context menu, it must have at least one menu item created with theuimenu功能。

Version History

在R2006a之前引入