Create button group to manage radio buttons and toggle buttons
bg = uibuttongroup creates a button group in the current figure and
ButtonGroup object. If there is no figure available,
MATLAB® calls the
figure function to create one.
bg = uibuttongroup(
specifies button group property values using one or more name-value pair
bg = uibuttongroup(
creates the button group in the specified parent container. The parent container can be
a figure created with either the
uifigure function, or a child container
such as a panel. Property values for
uibuttongroup vary slightly
depending on whether the app is created with the
uifigure function. For more information, see Name-Value Pair Arguments.
Code Response to Radio Button Selection
Show the previous and current button selections in the MATLAB Command Window when the user selects a different radio button in a button group.
Copy and paste this code into the Editor and run it to see how it works.
function myui bg = uibuttongroup('Visible','off',... 'Position',[0 0 .2 1],... 'SelectionChangedFcn',@bselection); % Create three radio buttons in the button group. r1 = uicontrol(bg,'Style',... 'radiobutton',... 'String','Option 1',... 'Position',[10 350 100 30],... 'HandleVisibility','off'); r2 = uicontrol(bg,'Style','radiobutton',... 'String','Option 2',... 'Position',[10 250 100 30],... 'HandleVisibility','off'); r3 = uicontrol(bg,'Style','radiobutton',... 'String','Option 3',... 'Position',[10 150 100 30],... 'HandleVisibility','off'); % Make the uibuttongroup visible after creating child objects. bg.Visible = 'on'; function bselection(source,event) disp(['Previous: ' event.OldValue.String]); disp(['Current: ' event.NewValue.String]); disp('------------------'); end end
bselection function displays the
NewValue properties of
Scrollable Button Group
Scrollable property enables scrolling within a
button group that has components outside its borders. Scrolling is only possible when
the button group is in a figure created with the
uifigure function. App Designer uses this type of figure for creating
Create a button group within a figure. Add six toggle buttons, with the first three lying outside the upper border of the button group.
fig = uifigure; bg = uibuttongroup(fig,'Position',[20 20 196 135]); tb1 = uitogglebutton(bg,'Position',[11 165 140 22],'Text','One'); tb2 = uitogglebutton(bg,'Position',[11 140 140 22],'Text','Two'); tb3 = uitogglebutton(bg,'Position',[11 115 140 22],'Text','Three'); tb4 = uitogglebutton(bg,'Position',[11 90 140 22],'Text','Four'); tb5 = uitogglebutton(bg,'Position',[11 65 140 22],'Text','Five'); tb6 = uitogglebutton(bg,'Position',[11 40 140 22],'Text','Six');
Enable scrolling by setting the
Scrollable property of the
button group to
'on'. By default, the scroll box displays at
bg.Scrollable = 'on';
parent — Parent container
Figure object (default) |
Panel object |
Tab object |
ButtonGroup object |
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.
uibuttongroup(Title="Options") specifies that the button
group title is
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
uibuttongroup("Title","Options") specifies that the button
group title is
The properties listed here are a subset of the available properties. For the full list, see ButtonGroup Properties.
BackgroundColor — Background color
[0.94 0.94 0.94] (default) | RGB Triplet | hexadecimal color code |
'b' | ...
Background color, specified as an RGB triplet, a hexadecimal color code, or one of the color options listed in the table.
RGB triplets and hexadecimal color codes are useful for specifying custom colors.
An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range
[0,1]; for example,
[0.4 0.6 0.7].
A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (
#) followed by three or six hexadecimal digits, which can range from
F. The values are not case sensitive. Thus, the color codes
Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.
|Color Name||Short Name||RGB Triplet||Hexadecimal Color Code||Appearance|
Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.
|RGB Triplet||Hexadecimal Color Code||Appearance|
SelectedObject — Currently selected radio button or toggle button
first radio button or toggle button in button group (default)
Currently selected radio button or toggle button, specified as a
RadioButton or a
ToggleButton object in
UIControl object in
Get the value of this property to determine which button is currently selected within the button group.
Set the value of this property to change the currently selected button. When
you change the selection using this property, MATLAB adjusts the
Value property for the other
buttons within the button group accordingly.
For example, suppose that your button group contains three radio buttons and
you set the
SelectedObject property to
radiobutton3. MATLAB sets the
Value property for each child
RadioButton as follows:
radiobutton1.Value = false;
radiobutton2.Value = false;
radiobutton3.Value = true;
In other words, setting the
SelectedObject property has
the same effect as setting the
Value property of the
buttons in the button group.
SelectionChangedFcn — Selection changed callback
'' (default) | function handle | cell array | character vector
Selection changed callback, specified as one of these values:
A function handle.
A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.
A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.
This callback executes when the user selects a different button within the
button group in the app. It does not execute if a radio or toggle button
Value property changes programmatically.
This callback function can access specific information about the user’s
interaction with the buttons. MATLAB passes this information in a
SelectionChangedData object as the second argument to your
callback function. In App Designer, the argument is called
event. You can query the object properties using dot
notation. For example,
event.NewValue returns the currently
selected button. The
object is not available to callback functions specified as character
The following table lists the properties of the
Previously selected button
Currently selected button
Component that executes the callback
For more information about writing callbacks, see Callbacks in App Designer.
Units — Units of measurement
Units of measurement, specified as one of the values in this table.
Distances in pixels are independent of your system resolution on Windows® and Macintosh systems:
On Linux® systems, the size of a pixel is determined by your system resolution.
These units are normalized with respect to the parent container. The lower-left corner of the container maps to
|Points. One point equals 1/72nd of an inch.|
These units are based on the default uicontrol font of the graphics root object:
To access the default uicontrol font, use
The recommended value is
'pixels', because most MATLAB app building functionality measures distances in pixels. You can create an
object that rescales based on the size of the parent container by parenting the object
to a grid layout manager created using the
uigridlayout function. For more information, see Lay Out Apps Programmatically.
A button group can contain any UI component type, but it only manages 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
SelectionChangedFcncallback function for the button group. You cannot define callbacks for the individual buttons.
To determine which radio button or toggle button is selected, query the
SelectedObjectproperty of the button group. You can execute this query anywhere in your code.
If you set the
Visibleproperty of a button group object to
'off', then any child objects it contains (buttons, other button groups, etc.) become invisible along with the parent button group. However, the
Visibleproperty value of each child object remains unaffected.
Version HistoryIntroduced before R2006a
R2022b: Change button group border width in
In apps created in App Designer and using the
BorderWidth property to change the border width of the
R2020b: Turn interaction off and on
To control whether a button group responds to user interaction, use the
Enable property. When the
is set to
'on', you can interact with the button group and with UI
components within it as long as they are enabled. When the
property is set to
'off', you cannot interact with the button group
or its content.
Enable property is supported only for button groups in App