Add Context Menus to a Programmatic App
Context menus appear when the user right-clicks on a figure or UI component. Follow
these steps to add a context menu to your UI:
(^1) Create the context menu object using the uicontextmenu function.
(^2) Add menu items to the context menu using the uimenu function.
(^3) Associate the context menu with a graphics object using the object's
UIContextMenu property.
Create the Context Menu Object
Use the uicontextmenu function to create a context menu object. The syntax is
handle = uicontextmenu('PropertyName',PropertyValue,...)
The parent of a context menu must always be a figure. Use the Parent property to
specify the parent of a uicontextmenu. If you do not specify the Parent property, the
parent is the current figure as specified by the root CurrentFigure property.
The following code creates a figure and a context menu whose parent is the figure. At this
point, the figure is visible, but not the menu.
fh = figure('Position',[300 300 400 225]);
cmenu = uicontextmenu('Parent',fh,'Position',[10 215]);
Note“Force Display of the Context Menu” on page 9-49 explains the use of the
Position property.
Add Menu Items to the Context Menu
Use the uimenu function to add items to the context menu. The items appear on the menu
in the order in which you add them. The following code adds three items to the context
menu created above.
mh1 = uimenu(cmenu,'Text','Item 1');
mh2 = uimenu(cmenu,'Text','Item 2');
mh3 = uimenu(cmenu,'Text','Item 3');
You can specify any applicable Uimenu when you define the context menu items. See the
uimenu reference page and “Add Menu Bar Menus” on page 9-38 for information about
9 Lay Out a Programmatic UI