How to Add a ButtonGroup in MATLAB?


In MATLAB, we can create different types of graphical user interface (GUI) components like Edit Fields, Hyperlinks, Buttons, and more. In this tutorial, we shall confine our attention to add a ButtonGroup in MATLAB.

What is a ButtonGroup in MATLAB?

MATLAB is an environment that provides an easy way of creating GUI applications without proper knowledge of computer programming. In MATLAB, there is a graphical user interface (GUI) component namely, ButtonGroup, which allows us to create a group of radio buttons or option buttons. One can select only one radio button within the group at a time.

This GUI component in MATLAB is particularly used in applications where we want allow users to select only one option from a set of options.

How to Create a ButtonGroup in MATLAB?

In MATLAB, there is a built-in function 'uibuttongroup' which is used to create a ButtonGroup in a MATLAB application. The step-by-step process of creating a ButtonGroup is described here.

Step (1) – Use the 'uibuttongroup' function to create a button group.

Step (2)– Create radio buttons or option button within the group. For this, use the 'uicontrol' function.

Step (3) – Set a callback function for each radio button. This function will execute when the user selects a specific radio button within the group.

Hence, the creating a ButtonGroup in a MATLAB application is a simple three step process.

Now, let us practically understand how to add a ButtonGroup in MATLAB with the help of examples.

Create a ButtonGroup in MATLAB with Default Properties

In MATLAB, to create a button group with default properties, the following syntax of the 'uibuttongroup' function is used:

bg = uibuttongroup();

Example

The following MATLAB program illustrates how to implement the MATLAB code to add a button group with default properties.

% MATLAB program to add ButtonGroup with default properties
% Create a ButtonGroup component
bg = uibuttongroup();
                
% Create four radio buttons in the ButtonGroup
R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'MATLAB', 'Position', [200 400 250 60]);
R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electrical', 'Position', [200 350 250 60]);
R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electronics', 'Position', [200 300 250 60]);
R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Computer Science', 'Position', [200 250 250 60]);

Output

Create a ButtonGroup within a Specified Parent Container

In MATLAB, to create a GroupButton within a specified parent container, we use the following syntax of the 'uibuttongroup' function:

bg = uibuttongroup(parent);

Example

The following MATLAB program demonstrates the implementation of MATLAB code for creating a ButtonGroup within a specified parent container (figure).

% MATLAB program to add ButtonGroup within a parent container
% Create a parent container
fig = uifigure('Name', 'Tutorials Point Courses', 'Position', [500, 500, 500, 300]);

% Create a ButtonGroup component
bg = uibuttongroup(fig, 'Position', [100, 50, 350, 200]);

% Create four radio buttons in the ButtonGroup
R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Tutorials', 'Position', [10 70 150 30]);
R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Video Lectures', 'Position', [10 40 150 30]);
R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'E-Books', 'Position', [170 70 150 30]);
R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Articles', 'Position', [170 40 150 30]);

Output

Create a ButtonGroup with Customized Appearance

The following syntax of the 'uibuttongroup' is used to create a button group with customized appearance and behavior:

bg = uibuttongroup(---, Name, Value,…);

Here, the name-value pairs are used to specify the custom properties of the ButtonGroup.

Example

The following example program demonstrates the implementation of this syntax.

% MATLAB program to add ButtonGroup with custom properties
% Create a parent container
fig = uifigure('Name', 'Tutorials Point Courses', 'Position', [500, 500, 500, 300]);

% Create a ButtonGroup component
bg = uibuttongroup(fig, 'Title', 'E-Books', 'Position', [100, 50, 350, 200]);

% Create four radio buttons in the ButtonGroup
R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'MATLAB', 'Position', [10 70 150 30]);
R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Python', 'Position', [10 40 150 30]);
R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Power Electronics', 'Position', [170 70 150 30]);
R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Digital Electronics', 'Position', [170 40 150 30]);

Output

Create a Scrollable ButtonGroup

We can also create a scrollable ButtonGroup in MATLAB. For this, we just set the 'scrollable' property of the ButtonGroup to 'on'. The following syntax is used,

bg.Scrollable = 'on';

Example

Consider the following MATLAB program to see the code implementation.

% MATLAB program to add a scrollable ButtonGroup
% Create a parent container
fig = uifigure('Name', 'Tutorials Point Courses', 'Position', [500, 500, 500, 300]);

% Create a ButtonGroup component
bg = uibuttongroup(fig, 'Position', [100, 50, 350, 200]);

% Create four radio buttons in the ButtonGroup
R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'MATLAB', 'Position', [200 400 250 60]);
R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electrical', 'Position', [200 350 250 60]);
R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electronics', 'Position', [200 300 250 60]);
R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Computer Science', 'Position', [200 250 250 60]);
R5 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Power Electronics', 'Position', [200 200 250 60]);
R6 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Digital Electronics', 'Position', [200 150 250 60]);

% Enable scrollable button group
bg.Scrollable = 'on';

Output

Conclusion

This is all about adding ButtonGroup in MATLAB. MATLAB provides a built-in function 'uibuttongroup' that allows us to create a group of radio buttons in our MATLAB application. In this tutorial, we explained "what is a button group" and how we can create different kinds of button groups with different properties in MATLAB.

Updated on: 07-Sep-2023

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements