Documentation

uicontextmenu

Create context menu

A uicontextmenu is a context menu that appears when the user right-clicks on a graphics object or UI component. The uicontextmenu function creates a context menu and sets any required properties.

Syntax

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

Description

example

c = uicontextmenu creates a uicontextmenu 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.

example

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

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

Examples

collapse all

Context Menu Attached to a Plot Line

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;

    % Set c to be the plot line's UIContextMenu
    plotline.UIContextMenu = c;

    % Create 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 = '-';
            end
        end
end

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

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

Context Menu with Submenus

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 top-level menu item
    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];
        end
    end
end

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 — Parent figurefigure

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 Uicontextmenu Properties.

'Callback' — Callback function that executes when user selects the uicontextmenu'' (default) | function handle | cell array | string

Callback function that executes when user selects the uicontextmenu, specified as one of these values:

  • Function handle

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

  • String that is a valid MATLAB expression. MATLAB evaluates this expression in the base workspace.

For more information about specifying a callback property value as a function handle, cell array, or string, see How to Specify Callback Property Values.

Example: @myfun

Example: {@myfun,x}

Data Types: function_handle | cell | char

'Children' — Children of uicontextmenuempty GraphicsPlaceholder array (default) | 1-D array of uimenu objects

Children of uicontextmenu, returned as an empty GraphicsPlaceholder or a 1-D array of uimenu objects. The children of uicontextmenus are other uimenus that function as submenus.

You cannot add or remove children using the Children property of the uicontextmenu. Use this property to view the list of children or to reorder the children. 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. For more information, see the HandleVisibility property description.

More About

collapse all

Tips

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

  • You create at least one uimenu and set its parent to be the uicontextmenu. Here is an example:

    c = uicontextmenu;
    m = uimenu('Parent',c,'Label','Disable');
    

  • You assign the uicontextmenu to be another component's UIContextMenu property value. Here is an example:

    c = uicontextmenu;
    b = uicontrol('UIContextMenu',c);
    b must be a descendant of the same figure as c.

Introduced before R2006a

Was this topic helpful?