Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

uibuttongroup

Create button group to manage radio buttons and toggle buttons

Syntax

  • bg = uibuttongroup
  • bg = uibuttongroup(Name,Value)
    example
  • bg = uibuttongroup(parent)
  • bg = uibuttongroup(parent,Name,Value)
    example

Description

bg = uibuttongroup creates a button group in an existing traditional figure and returns the button group object, bg. If there is no traditional figure available, then MATLAB® creates a new one to serve as the parent.

example

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

bg = uibuttongroup(parent) creates a button group and designates a specific parent. The parent argument can be a traditional figure, a UI figure, panel, tab, or another button group.

example

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

Examples

collapse all

Show the previous and current button selections in the MATLAB Command Window when the app user selects a new 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)
       display(['Previous: ' event.OldValue.String]);
       display(['Current: ' event.NewValue.String]);
       display('------------------');
    end
end

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

After you create a button group, use dot notation to set and get button group properties.

Create radio buttons group in a panel within a UI figure window.

fig = uifigure;
panel = uipanel(fig);
bg = uibuttongroup(panel,...
                  'Title','Language Options',...
                  'Position', [50 50 123 106]);

              
% Create three radio buttons in the button group.
r1 = uiradiobutton(bg,...
                  'Text','English',...
                  'Position',[10 60 70 15]);
              
r2 = uiradiobutton(bg,...
                  'Text','French',...
                  'Position',[10 38 70 15]);

r3 = uiradiobutton(bg,...
                  'Text','German',...
                  'Position',[10 15 70 15]);

Get the current button group selection

bg.SelectedObject.Text
ans =

English

Change the button group position within the panel

bg.Position = [10 10 123 106];

Show the previous and current button selections in the MATLAB Command Window when the app user selects a new radio button in a button group.

Copy and paste this code into the Editor and run it to see how it works.

function myfunc
fig = uifigure;
bg = uibuttongroup(fig,...
                  'Title','Options',...
                  'Position', [100 100 123 106],...
                  'SelectionChangedFcn',@(bg,event) bselection(bg,event));

              
% Create three radio buttons in the button group.
r1 = uiradiobutton(bg,...
                  'Text','Option 1',...
                  'Position',[10 60 70 15]);
              
r2 = uiradiobutton(bg,...
                  'Text','Option 2',...
                  'Position',[10 38 70 15]);

r3 = uiradiobutton(bg,...
                  'Text','Option 3',...
                  'Position',[10 15 70 15]);
              
    function bselection(bg,event)
       display(['Previous: ', event.OldValue.Text]);
       display(['Current: ', event.NewValue.Text]);
       display('------------------');
    end
end 

The bselection function displays the Previous and Current properties of the event object.

Input Arguments

collapse all

Parent object of the button group, specified as a figure, UI figure, panel, tab, or button group object. If you do not specify a parent, the default parent is a traditional figure object.

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 single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

More About

collapse all

traditional figure

A traditional figure is one that MATLAB creates when you use GUIDE or the figure function. For more information on the difference between traditional figures and UI figures, see Graphics Support in App Designer.

UI figure

A UI figure is one that MATLAB creates when you use App Designer or the uifigure function. For more information on the difference between traditional figures and UI figures, see Graphics Support in App Designer.

Tips

  • A button group can contain any UI component type (except an ActiveX® control), 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

Was this topic helpful?