Documentation

This is machine translation

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

uicontextmenu

Create context menu

Syntax

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

Description

example

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.

example

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

    Note:   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.

Examples

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 = '-';
            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.

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];
        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 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.

collapse all

Callback function that executes when user selects the uicontextmenu, 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.

Example: @myfun

Example: {@myfun,x}

Data Types: function_handle | cell | char

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 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?