This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Create Toolbars for Programmatic Apps

Use the uitoolbar Function

Use the uitoolbar function to add a custom toolbar to your UI. Use the uipushtool and uitoggletool functions to add push tools and toggle tools to a toolbar. A push tool functions as a push button. A toggle tool functions as a toggle button. You can add push tools and toggle tools to the standard toolbar or to a custom toolbar.

Syntaxes for the uitoolbar, uipushtool, and uitoggletool functions include the following:

tbh = uitoolbar(fh,'PropertyName',PropertyValue,...)
pth = uipushtool(tnh,'PropertyName',PropertyValue,...)
tth = uitoggletool(tbh,'PropertyName',PropertyValue,...)

The output arguments, tbh, pth, and tth are the handles, respectively, of the resulting toolbar, push tool, and toggle tool. See the uitoolbar, uipushtool, and uitoggletool reference pages for other valid syntaxes.

Subsequent topics describe commonly used properties of toolbars and toolbar tools, offer a simple example, and discuss use of the MATLAB® standard toolbar:

Commonly Used Properties

The most commonly used properties needed to describe a toolbar and its tools are shown in the following table.

Property

Values

Description

CData

3-D array of values between 0.0 and 1.0

n-by-m-by-3 array of RGB values that defines a truecolor image displayed on either a push button or toggle button.

HandleVisibility

on, off. Default is on.

Determines if an object's handle is visible in its parent's list of children. For toolbars and their tools, set HandleVisibility to off to protect them from operations not intended for them.

Separator

off, on. Default is off.

Draws a dividing line to left of the push tool or toggle tool

State

off, on. Default is off.

Toggle tool state. on is the down, or depressed, position. off is the up, or raised, position.

Tooltip

Character vector or string scalar

Text of the tooltip associated with the push tool or toggle tool.

For a complete list of properties and for more information about the properties listed in the table, see the Toolbar Properties, PushTool Properties, and ToggleTool Properties.

Toolbars

The following statements add a toolbar to a figure, and then add a push tool and a toggle tool to the toolbar. By default, the tools are added to the toolbar, from left to right, in the order they are created.

% Create the toolbar
fh = figure;
tbh = uitoolbar(fh);

% Add a push tool to the toolbar
a = [.20:.05:0.95];
img1(:,:,1) = repmat(a,16,1)';
img1(:,:,2) = repmat(a,16,1);
img1(:,:,3) = repmat(flip(a),16,1);
pth = uipushtool(tbh,'CData',img1,...
           'Tooltip','My push tool',...
           'HandleVisibility','off');
% Add a toggle tool to the toolbar
img2 = rand(16,16,3);
tth = uitoggletool(tbh,'CData',img2,'Separator','on',...
           'Tooltip','Your toggle tool',...
           'HandleVisibility','off');

fh is the handle of the parent figure.

th is the handle of the parent toolbar.

CData is a 16-by-16-by-3 array of values between 0 and 1. It defines the truecolor image that is displayed on the tool. If your image is larger than 16 pixels in either dimension, it may be clipped or cause other undesirable effects. If the array is clipped, only the center 16-by-16 part of the array is used.

Note

See the ind2rgb reference page for information on converting a matrix X and corresponding colormap, i.e., an (X, MAP) image, to RGB (truecolor) format.

Tooltip specifies the tooltips for the push tool and the toggle tool as My push tool and Your toggle tool, respectively.

In this example, setting the toggle tool Separator property to on creates a dividing line to the left of the toggle tool.

You can change the order of the tools by modifying the child vector of the parent toolbar. For this example, execute the following code to reverse the order of the tools.

oldOrder = allchild(tbh);
newOrder = flipud(oldOrder);
tbh.Children = newOrder;

This code uses flipud because the Children property is a column vector.

Use the delete function to remove a tool from the toolbar. The following statement removes the toggle tool from the toolbar. The toggle tool handle is tth.

delete(tth)

If necessary, you can use the findall function to determine the handles of the tools on a particular toolbar.

Note

After you have created a toolbar and its tools, set their HandleVisibility properties off by executing statements similar to the following:

toolbarhandle.HandleVisibility = 'off';
toolhandles = toolbarhandle.Children;
toolhandles.HandleVisibility = 'off';

Display and Modify the Standard Toolbar

You can choose whether or not to display the MATLAB standard toolbar (highlighted in red below). You can also add or delete tools from the standard toolbar.

Display the Standard Toolbar

Use the figure ToolBar property to display or hide the standard toolbar. Set ToolBar to 'figure' to display the standard toolbar. Set ToolBar to 'none' to hide it.

fh.ToolBar = 'figure';  % Display the standard toolbar
fh.ToolBar = 'none';    % Hide the standard toolbar

In these statements, fh is the handle of the figure.

The default ToolBar value is 'auto', which uses the MenuBar property value.

Modify the Standard Toolbar

Once you have the handle of the standard toolbar, you can add tools, delete tools, and change the order of the tools.

Add a tool the same way you would add it to a custom toolbar. This code gets the handle of the standard toolbar and adds a toggle tool to it.

tbh = findall(fh,'Type','uitoolbar');
tth = uitoggletool(tbh,'CData',rand(20,20,3),...
            'Separator','on',...
            'HandleVisibility','off');

To remove a tool from the standard toolbar, determine the handle of the tool to be removed, and then use the delete function to remove it. The following code deletes the toggle tool that was added to the standard toolbar above.

delete(tth)

If necessary, you can use the findall function to determine the handles of the tools on the standard toolbar.

See Also

| |

Related Topics