Create toggle tool in toolbar
creates a toggle tool in the
toolbar of the current figure and returns the
tt = uitoggletool
The current figure must be one created with the
figure function. If
the current figure does not have a child toolbar, then MATLAB® creates one in the current figure to serve as the parent. If a figure created
figure function does not exist, then MATLAB creates one and calls the
uitoolbar function to create a
toolbar to serve as the parent.
Toggle tools have two states:
state of the button changes every time you click it.
Create a figure by calling the
figure function. Add a toolbar
to the figure. It appears below the default figure toolbar.
f = figure; tb = uitoolbar(f);
Create a toggle tool in the toolbar. Create a 16-by-16-by-3 array of zeroes to be
its icon. Set the
CData property value to this array to display a
black square icon in the toggle tool.
tt = uitoggletool(tb); ttImage = zeros(16,16,3); tt.CData = ttImage;
Create a toggle tool that changes its icon and the background color of a UI figure each time you click it.
First, create a program file called
toggleColor.m. Within the
Create a UI figure.
Create a toolbar in the UI figure.
Add a toggle tool to the toolbar.
Create a blue truecolor image array. Set the toggle tool
CData property value to it.
ClickedCallback property to a function handle that
references a callback function called
Create a callback function called
toggleFigureColor. In it,
query the value of the
State property for the toggle tool. If the
'on', change the background color of the figure to blue
and make the toggle tool black. Similarly, if the state is
then change the figure background color to black and the make the toggle tool
function toggleColor fig = uifigure; tb = uitoolbar(fig); tt = uitoggletool(tb); ttImage = zeros(16,16,3); ttImage(:,:,3) = ones(16); tt.CData = ttImage; tt.ClickedCallback = @toggleFigureColor; function toggleFigureColor(src,event) state = src.State; if strcmp(state,'on') fig.Color = 'blue'; tt.CData = zeros(16,16,3); else fig.Color = 'black'; tt.CData = ttImage; end end end
toggleColor. Click the toggle tool to change the background
color of the figure.
parent— Parent toolbar
Parent toolbar, specified as a
Toolbar object. Use this property
to specify the parent toolbar when creating a toggle tool or to move an existing tool to
a different tool bar.
If a parent toolbar is not specified, then MATLAB creates a toggle tool in the toolbar of the current figure. The current
figure must be one created with the
figure function. If the current
figure does not have a child toolbar, then MATLAB creates one in the current figure to serve as the parent. MATLAB does not create the toggle tool in the default figure tool bar.
If a figure created with the
figure function does not exist,
then MATLAB creates one and calls the
uitoolbar function to
create a toolbar that serves as the parent.
If you add multiple push tools or toggle tools to a toolbar, they are added left to right, in the order that they are created.
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
'Separator','on'sets the separator line mode to
The properties listed here are only a subset. For a complete list, see ToggleTool Properties.
'State'— Toggle tool state
'off'(default) | on/off logical value
Toggle button state, specified as
'on', or as numeric or logical
'on' is equivalent to
'off' is equivalent to
false. Thus, you can
use the value of this property as a logical value. The value is stored as an on/off
logical value of type
When this property value is
'on', the toggle button appears in
the down (depressed) position. When this property value is
the toggle button appears in the up position. Changing the state causes the
function to execute.
Toolbar objects (and their child
ToggleTool objects) do not appear in figures whose
WindowStyle property is
'modal'. If a figure containing a toolbar child has its
WindowStyle changed to
'modal', the toolbar child
still exists in the
Children property of the figure.
However, the toolbar does not appear while
WindowStyle is set to