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.
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
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:
- 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 calledonButtonDown
. - Create a callback function called
onButtonDown
. In it, determine if the selection is a right-click by querying theSelectionType
property of the UI figure. When a right-click occurs, get the _x_- and _y_-coordinates of the mouse pointer from theCurrentPoint
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.
Input Arguments
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