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.
Type: double | integer
menulabel
Test displayed for h.
Type: string
property, value
'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');