Main Content

open

Open context menu at location within UI figure

Since R2020a

Description

open(cm,x,y) 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,coord) 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(cm,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 the ButtonDownFcn property.

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

  • Create UI axes in a UI figure and plot data in the axes.

  • Create a context menu with one submenu in the UI figure.

  • Set the WindowButtonDownFcn property to a callback function called onButtonDown.

  • Create a callback function called onButtonDown. In it, determine if the selection is a right-click by querying the SelectionType property of the UI figure. When a right-click occurs, get the x- and y-coordinates of the mouse pointer from the CurrentPoint property. The x- and y-coordinates are the first and second elements of the vector it returns. Then, open the context menu at those coordinates. When other selection types occur, display a message in the Command Window.

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.

openContextMenu

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

  • Close the context menu by pressing a key or clicking your mouse outside of the context menu. You cannot close a context menu programmatically.

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