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.
- menulabel
- Text displayed for h.
- property, value
-
- 'callback'
- Callback function that is triggered when interacting with h. If value is a functionhandle, it must be a function that takes 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.
- 'enable'
- Specifies if h is enabled. Valid values are 'on' (default) and 'off'.
- 'parent'
- Specifies the parent object which is either a uimenu or uimenuitem object.
- '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.
- '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 uimenuitem created. The 'position' of h is in the order it is created and cannot be changed once it is created.
Examples
%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});
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');