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.
creates a toggle tool with property values specified using one or more name-value pair
arguments. Specify name-value pairs with either of the previous syntaxes.
tt = uitoggletool(___,
Create Toggle Tool in Figure Toolbar
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 Toggle Tool That Changes UI Figure Color
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
Iconproperty value to it.
ClickedCallbackproperty to a function handle that references a callback function called
Create a callback function called
toggleFigureColor. In it, query the value of the
Stateproperty for the toggle tool. If the state is
'on', change the background color of the figure to blue and make the toggle tool black. Similarly, if the state is
'off', then change the figure background color to black and the make the toggle tool blue.
function toggleColor fig = uifigure; tb = uitoolbar(fig); tt = uitoggletool(tb); ttImage = zeros(16,16,3); ttImage(:,:,3) = ones(16); tt.Icon = ttImage; tt.ClickedCallback = @toggleFigureColor; function toggleFigureColor(src,event) state = src.State; if strcmp(state,'on') fig.Color = 'blue'; tt.Icon = zeros(16,16,3); else fig.Color = 'black'; tt.Icon = 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.
Specify optional pairs of arguments as
the argument name and
Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
'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.
Icon — Icon source or file
'' (default) | character vector | string scalar |
n-by-3 truecolor image array
Icon source or file, specified as a character vector, string scalar, or an
n-by-3 truecolor image array. If you
specify a character vector or string scalar, it can be an image file name on the
MATLAB path or a full path to an image file. If you plan to share your app with
others, put the image file on the MATLAB path to facilitate app packaging. Supported image formats include JPEG,
PNG, GIF, and SVG.
If you specify an
n-by-3 array, it is
interpreted as a truecolor image array. For more information about truecolor image
arrays, see Image Types.
If the image you specify is larger than 16-by-16 pixels, then the
Icon property scales the image down so that the entire image fits within the tool. If the image you specify is smaller than 16-by-16 pixels, it is not scaled up to fit the available space.
Icon property is supported only in App Designer and
uifigure-based apps. If the
CData properties are both set, then the
CData property is ignored.
'icon.png' specifies an image file on the MATLAB path.
'C:\Documents\icon.png' specifies a full path to an image file.
Toolbarobjects (and their child
ToggleToolobjects) do not appear in figures whose
WindowStyleproperty is set to
'modal'. If a figure containing a toolbar child has its
'modal', the toolbar child still exists in the
Childrenproperty of the figure. However, the toolbar does not appear while
WindowStyleis set to
Version HistoryIntroduced before R2006a
R2020b: Specify custom icons
In apps created in App Designer and
uifigure-based apps, to add a
custom icon to a toggle tool, set the
Icon property. You can specify
Icon property as an image file or an
n-by-3 truecolor array.
Icon property is recommended over the