uicontextmenuitem
Creates a menu item in a context menu which is displayed with a right-mouse click for a figure, uitab, or select uicontrol objects.
Syntax
h = uicontextmenuitem(parent, 'label', menulabel)
h = uicontextmenuitem(parent, 'label', menulabel, property, value, ...)
Inputs
- parent
- Handle of a container object, which is a uicontextmenu object or a uicontextmenuitem, if h is part of a cascading menu.
- menulabel
- Test displayed for h.
- property, value
-
- 'callback'
- Callback function that will be triggered when interacting with h. If value is a functionhandle, it must be a function that takes two argument: first is the handle of the object, second is the event data. If value is a string, it must represent a function handle or a function name. If value is a cell, it must contain the function name/function handle in the first cell element and parameters to pass to callback function in the additional elements.
- 'enable'
- Specifies if h is enabled. Valid values are 'on'(default) and 'off'.
- 'parent'
- Specifies the parent (uicontextmenu/uicontexmenuitem).
- 'position'
- Relative index of h in parent, where index is 1-based. uicontextmenuitem is created in the order they are defined or in the 'position' that has been specified at creation. Once the contextmenuitems have been created, their positions cannot be changed.
- 'separator'
- Valid values are 'off'(default) and 'on'. If specified as 'on', separator is added before h, if it is not the first item in the parent.
- 'tag'
- User-defined string to tag graphical control objects.
- 'userdata'
- User-defined numerical data.
- 'visible'
- Specifies if h is visible. Valid values are 'on'(default) and 'off'.
Outputs
- h
- Handle of the uicontextmenuitem created.
Examples
function outfunc1 = func1(h,callstate) % Callback for menu1
disp('Menu item 1 callback')
end
function outfunc2 = func2(h,callstate,argument1,argument2) % Callback for menu2
disp('Menu item 2 callback')
end
f = figure;
c = uicontextmenu(f);
% Creates individual context menu items
menu1 = uicontextmenuitem('parent', c, 'label', 'Do something', 'callback', '@func1');
menu2 = uicontextmenuitem(c, 'label', 'Do something else', 'callback', {@func2, 'foo', 1});
function outfunc1 = func1(h,callstate) % Callback for menu1
disp('Menu 1 callback')
end
function outfunc2 = func2(h,callstate,argument1,argument2) % Callback for menu2
disp('Menu 2 callback')
end
global buttongroup;
function changecolor(h,c) % Callback for cascading (submenu) menus
global buttongroup;
label = get(h,'label');
if strcmp(label,'Red') == 1
set(buttongroup,'backgroundcolor','red')
elseif strcmp(label,'Blue') == 1
set(buttongroup,'backgroundcolor','blue')
elseif strcmp(label,'Default') == 1
set(bgh,'backgroundcolor','transparent')
end
end
f = figure;
buttongroup = uicontrol('style','buttongroup','string', 'ButtonGroup','position',[0.6 0.1 0.2 0.3],'units','normalized');
button = uicontrol('parent', buttongroup, 'style', 'pushbutton', 'string', 'Test');
c = uicontextmenu(button);
% Creates individual context menu items
menu1 = uicontextmenuitem('parent', c, 'label', 'Do something', 'callback', '@func1');
menu2 = uicontextmenuitem(c, 'label', 'Do something else', 'callback', {@func2, 'foo', 1});
menu3 = uicontextmenuitem('parent', c, 'Label', 'Change color', 'separator', 'on')
% Creates cascading menu items for menu3
menu3_1 = uicontextmenuitem('Parent',menu3,'Label','Red','Callback',@changecolor, 'position', 2);
menu3_2 = uicontextmenuitem('Parent',menu3,'Label','Blue','Callback',@changecolor);
menu3_3 = uicontextmenuitem('Parent',menu3,'Label','Default','Callback',@changecolor, 'separator', 'on');