Create container object to exclusively manage radio buttons and toggle buttons
b = uibuttongroup
b = uibuttongroup(Name,Value,...)
b = uibuttongroup(parent)
b = uibuttongroup(parent,Name,Value,...)
b = uibuttongroup creates a uibuttongroup in an existing figure and returns the uibuttongroup object, b. If there is no figure available, then MATLAB® creates a new figure to serve as the parent.
b = uibuttongroup(Name,Value,...) creates a uibuttongroup and specifies one or more uibuttongroup property names and corresponding values. Use this syntax to override the default uibuttongroup properties.
b = uibuttongroup(parent,Name,Value,...) creates a uibuttongroup with a specific parent and one or more uibuttongroup properties.
Uibuttongroups are containers that group UI components together and manage exclusive selection of radio buttons and toggle buttons. These are some of the important characteristics of the uibuttongroups and some recommended best practices:
Uibuttongroups can contain any UI component type (except anActiveX® control), but it only manages the selection of radio buttons and toggle buttons.
Define a SelectionChangedFcn callback function for the uibuttongroup to make the GUI respond when the user selects a radio button or toggle button that is inside the button group. Do not define callbacks for in the individual buttons.
Query the SelectedObject property of the uibuttongroup to determine which radio button or toggle button is currently selected. You can execute this query anywhere in your code that can access the uibuttongroup object.
This example creates a uibuttongroup with three radio buttons. It manages the radio buttons with the SelectionChangedFcn callback, bselection.
When the user selects a new radio button, the bselection function displays the previous and current selection.
Copy and paste this code into the Editor and run it to see how it works.
function mygui 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,callbackdata) display(['Previous: ' callbackdata.OldValue.String]); display(['Current: ' callbackdata.NewValue.String]); display('------------------'); end end
The bselection function displays the OldValue and NewValue properties of the callbackdata object. MATLAB passes the callbackdata object to this callback function when you specify the SelectionChangedFcn property as a function handle (or a cell array containing a function handle). See SelectionChangedFcn for more information.
If you set the Visible property of a uibuttongroup object to 'off', then any child objects it contains (buttons, other button groups, etc.) become invisible along with the parent uibuttongroup. However, the Visible property value of each child object remains unaffected.