Main Content

uicontextmenu

Create context menu component

Description

cm = uicontextmenu creates a context menu in the current figure and returns the ContextMenu object. If a figure does not exist, then MATLAB® calls the figure function 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 child Menu object within the context menu.

example

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

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

collapse all

Create a context menu with two menu items in a UI figure. Assign the context menu to the figure itself by setting the ContextMenu property of the figure to the ContextMenu object. To view the context menu, right-click anywhere in the figure window.

fig = uifigure;

cm = uicontextmenu(fig);
m1 = uimenu(cm,'Text','Menu1');
m2 = uimenu(cm,'Text','Menu2');

fig.ContextMenu = cm;

Context menu with two options: "Menu1" and "Menu2"

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: "Option 1" and "Option 2"

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","Linda");

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 a MenuSelectedFcn callback function to execute when a user selects 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 the ContextMenu property of each node to the ContextMenu object.

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

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

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

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

function expandSingle(src,event,f)
node = f.CurrentObject;
expand(node)
end

function expandAll(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 "Cyclists" node. The "Expand" menu option is highlighted, and there is a submenu with options "This Node" and "All Nodes".

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 a ContextMenuOpeningFcn callback 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.

The context menu with a menu item called "Menu1" displays on the plot line.

Input Arguments

collapse all

Parent figure, specified as a Figure object created with either the uifigure or figure function. If a parent figure is not specified, then MATLAB calls the figure function to create one that serves as the parent.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is 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 enclose Name in quotes.

Example: uicontextmenu("ContextMenuOpeningFcn",@myfunction) specifies myfunction to 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, see ContextMenu 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, see Specify a Callback Function.

Context menu children, returned as an empty GraphicsPlaceholder or a vector of Menu objects.

You cannot add or remove children using the Children property. 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, Menu1 appears as the first menu option.

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

cm.Children returns a list of the menu items in the reverse order.

cm.Children
ans = 

  3×1 Menu array:

  Menu    (Menu3)
  Menu    (Menu2)
  Menu    (Menu1)

Objects with the HandleVisibility property set to 'off' are not listed in the Children property.

To add a child to this list, set the Parent property of another Menu object to this ContextMenu object.

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 the open function. The context menu must be the child of a figure created with the uifigure function. To display the context menu, it must have at least one menu item created with the uimenu function.

Version History

Introduced before R2006a