uicontextmenuitem

Creates a menu item in a context menu that is displayed with a right-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.
Type: double | integer
menulabel
Test displayed for h.
Type: string
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 at least two arguments: 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. After it is executed, createfcn cannot be interrupted.
Type: cell | functionhandle | string
'createfcn'
Function that is triggered when h is created. If value is a function handle, it must be a function that takes at least two arguments. The first argument is the handle of the uicontrol. The second argument is the event data to the uicontrol, which is ignored for createfcn. 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.
Type: cell | functionhandle | string
'deletefcn'
Function that is triggered when h is deleted. If value is a function handle, it must be a function that takes at least two arguments. The first argument is the handle of the uicontrol. The second argument is the event data to the uicontrol, which is ignored for createfcn. 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. After it is executed, createfcn cannot be interrupted.
Type: cell | functionhandle | string
'enable'
Specifies if h is enabled. Valid values are 'on'(default) and 'off'.
Type: off_on
'parent'
Specifies the parent (uicontextmenu/uicontexmenuitem).
Type: scalar
'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.
Type: positive integer
'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.
Type: off_on
'tag'
User-defined string to tag graphical control objects.
Type: string
'userdata'
User-defined numerical data.
Type: complex | mtx | scalar
'visible'
Specifies if h is visible. Valid values are 'on'(default) and 'off'.
Type: on_off

Outputs

h
Handle of the uicontextmenuitem created.

Examples

Create a context menu for a figure:
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});
Create a cascading (submenu) context menu for a button:
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');