Create context menu

Use this function only with GUIDE, or with figures created using the figure function.



c = uicontextmenu creates a context menu in the current figure and returns the ContextMenu object as c. If a figure does not exist, then MATLAB® calls the figure function to create one.

c = uicontextmenu(Name,Value) creates a context menu with property values specified using one or more name-value pair arguments. Use this syntax to override the default property values.


c = uicontextmenu(parent) creates a context menu in the specified parent figure.


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

  • Assign the context menu 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 for the context menu and one or more name-value pair arguments.


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 context menu 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 created with the figure. 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

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.

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 (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.

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 context menu to a component using the UIContextMenu property. The assigned component must be a child of the same figure as the context menu.

  • 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