uitoolbar

Create toolbar in figure

Description

tb = uitoolbar creates a toolbar in the current figure and returns the Toolbar object. If a figure created with the figure function does not exist, then MATLAB® creates one to serve as the parent.

example

tb = uitoolbar(parent) creates a toolbar in the specified parent figure. The parent container can be a figure created with either the uifigure or figure function.

tb = uitoolbar(___,Name,Value) creates a toolbar with property values specified using one or more name-value pair arguments. Specify name-value pairs with either of the previous syntaxes.

Examples

collapse all

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);

Figure that displays the default toolbar and another empty toolbar below it.

Create a push tool in the toolbar. Read a new icon from the indexed image file, matlabicon.gif. Convert the indexed image to an RGB truecolor image array. Add the icon to the push tool it by setting the CData property to the truecolor image array.

pt = uipushtool(tb);

[img,map] = imread(fullfile(matlabroot,...
            'toolbox','matlab','icons','matlabicon.gif'));
ptImage = ind2rgb(img,map);

pt.CData = ptImage;

Figure that displays the default toolbar and another toolbar below it that contains a push tool with the MathWorks logo.

Change the left-to-right order of tools in a toolbar. In this case, reverse the order of a push tool and toggle tool that are in a UI figure toolbar.

Create a UI figure. Add a toolbar to it. Then, add a push tool and toggle tool to the toolbar.

fig = uifigure;
tb = uitoolbar(fig);
pt = uipushtool(tb);
tt = uitoggletool(tb);

UI figure with a push tool and toggle tool in a toolbar that both display the default icon.

Create a push tool in the toolbar. Read a new icon from the indexed image file, matlabicon.gif. Convert the indexed image to an RGB truecolor image array. Add the icon to the push tool by setting the CData property to the truecolor image array.

[img,map] = imread(fullfile(matlabroot,...
            'toolbox','matlab','icons','matlabicon.gif'));
ptImage = ind2rgb(img,map);
pt.CData = ptImage;

Membrane logo added to the push tool. The push tool is to the left of the toggle tool.

Create a blue truecolor image array to be the icon for the toggle tool. Set the CData property value to this array to display a blue square icon in the toggle tool.

ttImage = zeros(16,16,3);
ttImage(:,:,3) = ones(16);
tt.CData = ttImage;

Blue square added to the toggle tool.

Query the Children property of the toolbar. The order of the children returned in this array reflects the right-to-left order of the tools displayed in the toolbar. The toggle tool is the right-most tool and appears at the top of the list (the first element of the array).

oldToolOrder = tb.Children
oldToolOrder = 

  2×1 graphics array:

  ToggleTool
  PushTool

Reverse the order of the tools by calling the flipud function to flip the order of the elements in the array returned by tb.Children. Set the Children property value to this new tool order. The push tool now appears to the right of the toggle tool in the toolbar.

newToolOrder = flipud(oldToolOrder);
tb.Children = newToolOrder;

Blue toggle tool appears to the left of the push tool in the toolbar.

Input Arguments

collapse all

Parent figure, specified as a Figure object created with either the uifigure or figure function. If a parent figure is not specified, then MATLAB calls the figure function to create one that serves as the parent.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is 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 Name1,Value1,...,NameN,ValueN.

Example: 'Visible','off' sets the visibility of the toolbar to 'off'.

Note

The properties listed here are only a subset. For a complete list, see Toolbar Properties.

State of visibility, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and '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 matlab.lang.OnOffSwitchState.

  • 'on' — Display the object.

  • 'off' — Hide the object without deleting it. You still can access the properties of an invisible UI component.

To make your app start faster, set the Visible property to 'off' for all UI components that do not need to appear at startup.

Toolbar children, returned as an empty GraphicsPlaceholder or a 1-D array of component objects. The children of Toolbar objects are PushTool and ToggleTool objects.

You cannot add or remove children using the Children property. Use this property to view the list of children or to reorder the children. The order of the children in this array reflects the right-to-left order of the tools displayed in the toolbar. Meaning that the right-most tool is at the top of the list and the left-most tool is at the bottom of the list. For example, this tool order returned by the Children property indicates that the push tool appears to the left of the toggle tool in the toolbar.

toolOrder = tb.Children
toolOrder = 

  2×1 graphics array:

  ToggleTool
  PushTool

To add a child to this list, set the Parent property of the child component to the uitoolbar object.

Objects with the HandleVisibility property set to 'off' are not listed in the Children property.

Tips

  • Toolbars can contain push tools or toggle tools. Push tools behave like push buttons. When you click them, they appear to depress until you release the mouse button. Toggle tools have two states: 'off' or 'on'. The state of the button changes every time you click it.

  • Toolbar objects (and their child PushTool and ToggleTool objects) do not appear in figures whose WindowStyle property is set to '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 'modal'.

See Also

Functions

Properties

Introduced before R2006a