uitogglebutton

Create toggle button component

Description

tb = uitogglebutton creates toggle button within a button group and returns the ToggleButton object. MATLAB® calls the uifigure function to create the parent figure of the button group.

example

tb = uitogglebutton(parent) creates the toggle button within the specified button group. The button group must be the child of a Figure created with the uifigure function, or must be parented to a child container of the figure: Tab, Panel, ButtonGroup, or GridLayout.

example

tb = uitogglebutton(___,Name,Value) specifies ToggleButton properties using one or more Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Create toggle buttons by first creating a figure window and a button group to contain the buttons.

fig = uifigure('Position',[680 678 398 271]);
bg = uibuttongroup(fig,'Position',[137 113 123 85]);

Create three toggle buttons and specify the location of each.

tb1 = uitogglebutton(bg,'Position',[10 50 100 22]);
tb2 = uitogglebutton(bg,'Position',[10 28 100 22]);
tb3 = uitogglebutton(bg,'Position',[10 6 100 22]);

Change the text associated with each toggle button.

tb1.Text = 'English';
tb2.Text = 'French';
tb3.Text = 'German';

Change the toggle button selection to German programmatically.

tb3.Value = true;

Determine the font name of the German toggle button text.

font = tb3.FontName
font =

Helvetica

Input Arguments

collapse all

Parent container, specified as a ButtonGroup object. The ButtonGroup must be parented to a Figure created using the uifigure function, or to a child container of a uifigure, such as: Tab, Panel, ButtonGroup, or GridLayout.

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: 'Text', 'French' specifies that the text “French” displays on the toggle button.

The properties listed here are a subset of the available properties. For the full list, see ToggleButton Properties.

State of the toggle button specified as 0 (unpressed) or 1 (depressed). Within a given button group, only one toggle button can be selected (depressed) at a time. When the Value property is set to 1, the toggle button appears depressed. The state of the first button added to a button group is 1, by default. Subsequent buttons added to the same button group have a default state of 0.

When the Value property of a ToggleButton changes to 1, the Value property of the previously selected ToggleButton changes to 0. In addition, the SelectedObject property value of the ButtonGroup is updated.

If you programmatically change the Value property of a ToggleButton to 0, MATLAB sets the Value property of the first ToggleButton added to the ButtonGroup to 1. If the first ToggleButton added is the one for which you programmatically set the Value property to 0, then MATLAB sets the Value property for the ToggleButton added to the ButtonGroup to 1.

Note

The first ToggleButton added to a ButtonGroup is not necessarily the first ToggleButton listed in the Children property of the ButtonGroup.

Button label, specified as a character vector, cell array of character vectors, string scalar, string array, or 1-D categorical array. Specify a character vector or string scalar to label the button with a single line of text. Use a cell array or string array to label the button with multiple lines of text. Each element in the array represents a separate line of text. If you specify this property as a categorical array, MATLAB uses the values in the array, not the full set of categories.

Icon source or file, specified as a character vector, a string scalar, or an m-by-n-by-3 truecolor image array. If you specify a file name, it can be an image file name on the MATLAB path or a full path to an image file. If you plan to share an app with others, put the image file on the MATLAB path to facilitate app packaging.

Supported image formats include JPEG, PNG, GIF, SVG, or m-by-n-by-3 truecolor image array. For more information about truecolor image arrays, see Image Types.

  • If the button text takes up all the space specified by the Position property value, then MATLAB does not display the icon.

  • If some room is available for the icon, then MATLAB scales down the image to fit, if necessary.

Example: 'icon.png' specifies an icon file on the MATLAB path.

Example: 'C:\Documents\icon.png' specifies a full path to an image file.

Location and size button, specified as a vector of the form [left bottom width height]. This table describes each element in the vector.

ElementDescription
leftDistance from the inner left edge of the button group to the outer left edge of the button
bottomDistance from the inner bottom edge of the button group to the outer bottom edge of the button
widthDistance between the right and left outer edges of the button
heightDistance between the top and bottom outer edges of the button

The Position values are relative to the drawable area of the button group. The drawable area is the area inside the borders of the button group and does not include the area occupied by the title.

All measurements are in pixel units.

Tips

  • Button groups can contain any UI component type, but can only manage the selection of radio buttons and toggle buttons.

  • To make your program respond when the app user selects a radio button or toggle button that is inside a button group. define a SelectionChangedFcn callback function for the ButtonGroup object. You cannot define callbacks for the individual buttons.

  • To determine which radio button or toggle button is selected, query the SelectedObject property of the ButtonGroup object. You can execute this query anywhere in your code.

  • If you set the Visible property of a ButtonGroup object to 'off', then any child objects it contains become invisible along with the parent ButtonGroup. However, the Visible property value of each child object remains unaffected.

Introduced in R2016a