uibuttongroup

Create button group to manage radio buttons and toggle buttons

Description

bg = uibuttongroup creates a button group in the current figure and returns the ButtonGroup object. If there is no figure available, MATLAB® calls the figure function to create one.

example

bg = uibuttongroup(Name,Value) specifies button group property values using one or more name-value pair arguments.

bg = uibuttongroup(parent) creates the button group in the specified parent container. The parent container can be a figure created with either the figure or 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 figure or uifigure function. For more information, see Name-Value Pair Arguments.

example

bg = uibuttongroup(parent,Name,Value) specifies the parent container and one or more properties values.

Examples

collapse all

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

The bselection function displays the OldValue and NewValue properties of event.

The 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 apps.

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 the top.

bg.Scrollable = 'on';

Input Arguments

collapse all

Parent container, specified as a figure created with either the figure or uifigure function, or a child container:

  • Panels, tabs and button groups can be containers in either type of figure.

  • Grid layouts can be containers only in figures created with the uifigure function.

Name-Value Pair Arguments

Example: 'Title','Options' specifies that the button group title is Options.

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments as Name1,Value1,...,NameN,ValueN.

  • For a list of properties available for apps created with the uifigure function or in App Designer, see ButtonGroup Properties.

  • For a list of properties available for apps created with the figure function, see ButtonGroup Properties.

Tips

  • 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 SelectionChangedFcn callback 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 SelectedObject property of the button group. You can execute this query anywhere in your code.

  • If you set the Visible property 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 Visible property value of each child object remains unaffected.

Introduced before R2006a