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 pair arguments. Specify name-value pairs with either of the previous syntaxes.

Examples

collapse all

Create a context menu with two submenus 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;

Create a context menu with actions appropriate to a tree component. Assign the context menu to the top-level tree nodes in a tree.

In a UI figure, create a tree with four top-level nodes and a set of nested nodes.

fig = uifigure;

tree = uitree(fig,'Position',[20 200 175 100]);

category1 = uitreenode(tree,'Text','Runners');
r1 = uitreenode(category1,'Text','Joe');
r2 = uitreenode(category1,'Text','Linda');

category2 = uitreenode(tree,'Text','Cyclists');
c1 = uitreenode(category2,'Text','Rajeev');

category3 = uitreenode(tree,'Text','Hikers');
h1 = uitreenode(category3,'Text','Jack');

category4 = uitreenode(tree,'Text','Swimmers');
s1 = uitreenode(category4,'Text','Logan');

Create a context menu with three menu items. For the last menu item, create four submenus.

cm = uicontextmenu(fig);
m1 = uimenu(cm,'Text','Expand All');
m2 = uimenu(cm,'Text','Collapse All');
m3 = uimenu(cm,'Text','Scroll To...');

sbm1 = uimenu(m3,'Text','Runners');
sbm2 = uimenu(m3,'Text','Cyclists');
sbm3 = uimenu(m3,'Text','Hikers');
sbm4 = uimenu(m3,'Text','Swimmers');

Then, 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;

Right-click any of the top-level tree nodes to view the context menu.

At this point, the code is still unfinished. The context menu appears when you right-click, but selecting menu items does not have any effect. To finish implementing the behavior of the menu items, create a MenuSelectedFcn callback function.

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 Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'ContextMenuOpeningFcn',@myfunction specifies myfunction to be the function that executes when the user interacts with the context menu.

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 How to Specify Callback Property Values.

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.

See Also

Functions

Properties

Introduced before R2006a