This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Create context menu


c = uicontextmenu
c = uicontextmenu(Name,Value)
c = uicontextmenu(parent)
c = uicontextmenu(parent,Name,Value)



c = uicontextmenu creates a context menu in the current figure and returns the uicontextmenu object as c. If no figure is available, then MATLAB® creates a new figure to serve as the parent.

c = uicontextmenu(Name,Value) specifies one or more uicontextmenu property names and corresponding values. Use this syntax to override the default uicontextmenu property values.


c = uicontextmenu(parent) creates a uicontextmenu and specifies the parent figure.


Specifying a parent alone does not make the context menu accessible in the UI. You must also take these steps:

  • Assign the uicontextmenu to a component using the UIContextMenu property.

  • Create at least one child uimenu for the uicontextmenu.

See the Tips section for more information.

c = uicontextmenu(parent,Name,Value) specifies a parent figure and one or more uicontextmenu properties.


collapse all

Specify a value for the line object’s UIContextMenu property to attach a uicontextmenu to that line. The context menu becomes visible when the user right-clicks on the line. For instance, create a program file called myprogram.m that creates a plot and attaches a uicontextmenu to the plot line:

function myprogram

    f = figure('WindowStyle','normal');
    ax = axes;
    x = 0:100;
    y = x.^2;

    plotline = plot(x,y);
    c = uicontextmenu;

    % Assign the uicontextmenu to the plot line
    plotline.UIContextMenu = c;

    % Create child menu items for the uicontextmenu
    m1 = uimenu(c,'Label','dashed','Callback',@setlinestyle);
    m2 = uimenu(c,'Label','dotted','Callback',@setlinestyle);
    m3 = uimenu(c,'Label','solid','Callback',@setlinestyle);

        function setlinestyle(source,callbackdata)
            switch source.Label
                case 'dashed'
                    plotline.LineStyle = '--';
                case 'dotted'
                    plotline.LineStyle = ':';
                case 'solid'
                    plotline.LineStyle = '-';

The context menu appears when the user right-clicks the plot line.

Selecting an item from the context menu changes the line style.

Specify the Parent property value of any uimenu to make it into a submenu. For instance, create a program file called myprogram2 that creates a uicontextmenu containing one top-level menu and two submenu items:

function myprogram2
    f = figure('WindowStyle','normal');
    c = uicontextmenu(f);

    % Assign the uicontextmenu to the figure
    f.UIContextMenu = c;

    % Create child menu of the uicontextmenu
    topmenu = uimenu('Parent',c,'Label','Change Color');
    % Create submenu items
    m1 = uimenu('Parent',topmenu,'Label','Red','Callback',@changecolor);
    m2 = uimenu('Parent',topmenu,'Label','Green','Callback',@changecolor);

    function changecolor(source,callbackdata)
        switch source.Label
            case 'Red'
                f.Color = [1.0 0.80 0.80];
            case 'Green'
                f.Color = [0.80 1.0 0.80];

The resulting context menu appears when the user right-clicks the mouse inside the figure.

Selecting a color from the context menu changes the color of the window.

Input Arguments

collapse all

Parent figure, specified as a figure object.

Name-Value Pair Arguments

Example: 'Callback',@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.

collapse all

Context menu 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. For example, 'disp(''hello world'')' calls the disp function. 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.

Data Types: function_handle | cell | char

Menu children, returned as an empty GraphicsPlaceholder or a 1-D array of Menu objects. The children of menus are other menus that function as submenus.

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 order of the displayed menu items.

To add a child to this list, set the Parent property of the child component to be the uicontextmenu object.

Objects with the HandleVisibility property set to 'off' do not list in the Children property.


The context menu becomes accessible in the UI when these requirements are met:

  • You assign the uicontextmenu to a component using the UIContextMenu property. The assigned component must be a child of the same figure as the uicontextmenu.

  • You create at least one child uimenu for the uicontextmenu.

For example:

f = figure;
c = uicontextmenu(f);
% Create a new component and assign the uicontextmenu to it
b = uicontrol(f,'UIContextMenu',c);
% Create a child menu for the uicontextmenu
m = uimenu('Parent',c,'Label','Disable');

Introduced before R2006a

Was this topic helpful?