open - Open context menu at location within UI figure - MATLAB (original) (raw)

Open context menu at location within UI figure

Syntax

Description

open([cm](#mw%5F2fd695e8-15fb-4969-8d85-c11b9ff848df),[x](#mw%5F61abb502-eb02-467b-968d-99afe9099faf),[y](#mw%5F610ba14f-fa67-4d70-bc60-188a04358a3b)) opens the context menu cm at the specified (x,y) coordinates within the UI figure that it is parented to. The coordinates are measured in pixels from the lower-left corner of the figure. The figure must be one that is created with the uifigure function.

example

open([cm](#mw%5F2fd695e8-15fb-4969-8d85-c11b9ff848df),[coord](#mw%5F6d95525b-db38-48df-9623-6672f4f71bb9)) specifies pixel coordinates as a two-element vector coord. For example,open(cm,[100 150]) opens context menu cm at the coordinates (100,150).

Examples

collapse all

Create a UI figure. Create a context menu with two submenus and assign it to the UI figure.

fig = uifigure;

cm = uicontextmenu(fig); m1 = uimenu(cm,'Text','Import Data'); m2 = uimenu(cm,'Text','Export Data');

fig.ContextMenu = cm;

Then, open the context menu at location (250,250).

Open an unassigned context menu when you right-click on a blank area of the UI figure it is parented to or on a graphics object that supports theButtonDownFcn property.

First, create a program file called openCtxtMenu.m. Within the program file:

function openCtxtMenu fig = uifigure; ax = uiaxes(fig); plot(ax,magic(5));

cm = uicontextmenu(fig); m = uimenu(cm,'Text','Menu1');

fig.WindowButtonDownFcn = @onButtonDown;

function onButtonDown(src,event)
    clickType = src.SelectionType;

    switch clickType
        case 'alt'
        x = src.CurrentPoint(1);
        y = src.CurrentPoint(2);
        open(cm,x,y)

        otherwise
        disp('Right-click to view context menu')
    end

end

end

Run the program file, then right-click on the UI axes or on a blank spot within the UI figure to open the context menu.

UI figure window with a UI axes. The mouse pointer is over the axes, and there is a context menu next to the pointer.

Input Arguments

collapse all

Context menu object created with the uicontextmenu function.

_x_-coordinate, specified as an integer in pixels from the left edge of the UI figure. If you specify a value that exceeds the width of the figure, then the context menu will not be visible.

_y_-coordinate, specified as an integer in pixels from the bottom edge of the figure. If you specify a value that exceeds the height of the figure, then the context menu will not be visible.

Pixel coordinates, specified as a two-element row vector of integer values.

Example: [100 150] specifies pixel coordinates(100,150).

Tips

Algorithms

ContextMenuOpeningFcn callback functions do not execute when you call the open function. The callback functions are triggered by user interactions only.

Version History

Introduced in R2020a