Main Content

uicontextmenu

Create context menu component

Description

cm= uicontextmenucreates a context menu in the current figure and returns theContextMenuobject. If a figure does not exist, then MATLAB®calls thefigurefunction to create one.

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

  • Assign the context menu to a UI component or graphics object in the same figure.

  • Create at least one childMenuobject within the context menu.

example

cm= uicontextmenu()creates a context menu in the specified parent figure. The parent can be a figure created with either theuifigureorfigurefunction.

example

cm= uicontextmenu(___,Name,Value)creates a context menu with property values specified using one or more name-value arguments. Specify name-value arguments with either of the previous syntaxes.

Examples

全部折叠

在UI图中使用两个菜单项创建上下文菜单。通过设置上下文菜单将上下文菜单分配给图形本身ContextMenuproperty of the figure to theContextMenuobject. To view the context menu, right-click anywhere in the figure window.

fig = uifigure; cm = uicontextmenu(fig); m1 = uimenu(cm,'文本','Menu1');m2 = uimenu(cm,'文本','Menu2');fig.ContextMenu = cm;

Context menu with two options:

Create a 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.

fig = uifigure; btn = uibutton(fig); cm = uicontextmenu(fig); m1 = uimenu(cm,"Text","Option 1");m2 = uimenu(cm,"Text","Option 2");btn.ContextMenu = cm;

Context menu for a button with two options:

Create a 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.

fig = uifigure; t = uitree(fig,"Position",[20 200 175 100]); category1 = uitreenode(t,"Text","Runners");r1 = uitreenode(category1,"Text","Joe");r2 = uitreenode(category1,"Text",“琳达”);category2 = uitreenode(t,"Text","Cyclists");c1 = uitreenode(category2,"Text","Rajeev");category3 = uitreenode(t,"Text","Hikers");h1 = uitreenode(category3,"Text","Jack");category4 = uitreenode(t,"Text","Swimmers");s1 = uitreenode (category4,"Text","Logan");

Tree with four collapsed top-level nodes

Create a context menu with one menu item and two submenus that users can click to expand a single tree node or all of the tree nodes. For each submenu, specify aMenuSelectedFcn回调函数执行当用户选择the menu option. Pass the relevant app object as input to each function to access app data from within the callback function.

cm = uicontextmenu(fig); m1 = uimenu(cm,"Text","Expand...");sbm1 = uimenu(m1,"Text","This Node",..."MenuSelectedFcn",{@expandSingle,fig}); sbm2 = uimenu(m1,"Text","All Nodes",..."MenuSelectedFcn",{@expandAll,t});

Assign the context menu to the top-level tree nodes by setting theContextMenuproperty of each node to theContextMenuobject.

category1.ContextMenu = cm; category2.ContextMenu = cm; category3.ContextMenu = cm; category4.ContextMenu = cm;

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

Define theexpandSinglefunction to also accept the UI figure object containing the tree, and use theCurrentObjectproperty of the figure to determine which tree node was clicked to bring up the context menu. Then, expand that node.

Define theexpandAllfunction to also accept the tree object, and expand all of the nodes in the tree.

functionexpandSingle(src,event,f) node = f.CurrentObject; expand(node)endfunctionexpandAll(src,event,t) expand(t)end

Save and run the script. Right-click any of the top-level tree nodes to view the context menu.

A context menu associated with the

Create a context menu that prints a message in the Command Window each time you open it.

Create a line plot in a traditional figure. Then, create a context menu with one menu item and assign it to the line plot. Create aContextMenuOpeningFcncallback function that displays output in the Command Window each time the context menu opens.

f = figure; p = plot(1:10); cm = uicontextmenu(f); m = uimenu(cm,"Text","Menu1");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.

上下文菜单带有菜单项,称为“菜单1”,在绘图行上显示。

Input Arguments

全部折叠

Parent figure, specified as aFigureobject created with either theuifigureorfigurefunction. If a parent figure is not specified, then MATLAB calls thefigurefunction to create one that serves as the parent.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

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

Example:uicontextmenu("ContextMenuOpeningFcn",@myfunction)specifiesmyfunctionto be the function that executes when the user opens the context menu.

Note

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

Context menu opening callback function, 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.

For more information about specifying a callback property value as a function handle, cell array, or character vector, seeSpecify a Callback Function.

上下文菜单孩子, returned as an emptyGraphicsPlaceholderor a vector ofMenuobjects.

You cannot add or remove children using theChildrenproperty. Use this property to view the list of children or to reorder the child menu items. The order of the children in this array reflects the reverse-order of how the menu items appear in an opened context menu.

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

fig = uifigure; cm = uicontextmenu(fig); m1 = uimenu(cm,'文本','Menu1');m2 = uimenu(cm,'文本','Menu2');m3 = uimenu(cm,'文本','Menu3');fig.ContextMenu = cm;
Context menu with three menu items.

CM.Childrenreturns a list of the menu items in the reverse order.

CM.Children
ans = 3×1 Menu array: Menu (Menu3) Menu (Menu2) Menu (Menu1)

带有的对象HandleVisibilityproperty set to'off'are not listed in theChildrenproperty.

To add a child to this list, set theParentproperty of anotherMenuobject to thisContextMenuobject.

Tips

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

    • Have at least one menu item.

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

  • To open a context menu programmatically, use theopenfunction. The context menu must be the child of a figure created with theuifigurefunction. To display the context menu, it must have at least one menu item created with theuimenufunction.

Version History

Introduced before R2006a