uimenuitem

Creates a menu item, in a menu bar, for a figure.

Syntax

h = uimenuitem(parent, 'label', menulabel)

h = uimenuitem(parent, 'label', menulabel, property, value, ...)

Inputs

parent
Handle of a uimenu object or a uimenuitem, if h is part of a cascading menu.
Type: double | integer
menulabel
Test displayed for h.
Type: string
property, value
'callback'
Callback function that is 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, and 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 the 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 object which is either a uimenu or uimenuitem object.
Type: scalar
'separator'
Valid values are 'off' (default) and 'on'. If specified as 'on', a 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 uimenuitem created. The 'position' of h is in the order it is created and cannot be changed once it is created.
Type: scalar

Examples

Creates a menu bar and menu items for a figure:
%Callback
function outfunc1 = func1(h,callstate)
    warndlg('Some modal warning')
end

% Callback
function outfunc2 = func2(h,callstate,argument1,argument2)
    disp('Menu item 2 callback')
end

f = figure;

% Test 1 Creating menu for a figure
m1 = uimenu('label', 'Menu1');
m2 = uimenu('label', 'Menu2', 'parent', f);

% Create menu items for each menu created
item1_m1 = uimenuitem('parent', m1, 'label', 'Foo1', 'callback', '@func1');
item1_m2 = uimenuitem(m2, 'label', 'Execute', 'callback', {@func2, 'foo', 1});
Creates a cascading (submenus) menu for a figure:
global bgh;

% Callback to change color
function changecolor(h,c)
  global bgh;
  label = get(h,'label');
  if strcmp(label,'Red') == 1
    set(bgh,'backgroundcolor','red')
  elseif strcmp(label,'Blue') == 1
    set(bgh,'backgroundcolor','blue')
  elseif strcmp(label,'Green') == 1
    set(bgh,'backgroundcolor','green')
  elseif strcmp(label,'Default') == 1
    set(bgh,'backgroundcolor',[250 250 180])
  end
end

% Callback
function outfunc1 = func1(h,callstate)
  warndlg('Some modal warning')
end

% Callback
function outfunc2 = func2(h,callstate,argument1,argument2)
  disp('Menu item 2 callback')
end

% Create some uicontrol elements
bgh = uicontrol('style','buttongroup','string', 'ButtonGroup', 'position',[0.6 0.1 0.2 0.3],'units','normalized');
pbh1 = uicontrol('parent',bgh,'string', 'Button','position',[0 0.5 0.2 0.3],'units','normalized');
rbh1 = uicontrol('parent',bgh,'style','radiobutton','string', 'Button','position',[0 0.5 0.2 0.3],'units','normalized');
set(bgh,'backgroundcolor',[250 250 180])

% Creating menu for a figure
m1 = uimenu('label', 'Menu1');
m2 = uimenu('label', 'Menu2');
m3 = uimenu('label', 'Menu3', 'enable', 'off');

item1_m1 = uimenuitem('parent', m1, 'label', 'Foo1', 'callback', '@func1');
item2_m1 = uimenuitem(m1, 'label', 'A very long menu description', 'callback', {@func2, 'foo', 1}, 'separator', 'off');
item3_m1 = uimenuitem('parent', m1, 'Label', 'Change color', 'separator', 'on')

item1_m2 = uimenuitem('parent', m2 ,'label', 'Foo2', 'callback', '@func1');

% Creating sub menus
submenu1 = uimenuitem('Parent',item3_m1,'Label','Red','Callback',@changecolor);
submenu2 = uimenuitem('Parent',item3_m1,'Label','Blue','Callback',@changecolor);
submenu3 = uimenuitem('Parent',item3_m1,'Label','Green','Callback',@changecolor);
submenu4 = uimenuitem('Parent',item3_m1,'Label','Default','Callback',@changecolor, 'separator', 'on');