Open In App

How to add a ButtonGroup in MATLAB ?

Last Updated : 14 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

ButtonGroup allows users to manage toggle buttons and radio buttons in the figure. ButtonGroup object can be created using uibuttongroup() method.

Different syntaxes of uibuttongroup() are

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

Now, let us discuss the above syntaxes in detail:

Using uibuttongroup()

  • It creates a button group in the current figure and returns the buttonGroup object.
  • In the following example, radio buttons are inserted in the button group.

Matlab




% Creating a ButtonGroup object
bg = uibuttongroup();
                
% Create three radio buttons in the 
% button group
r1 = uicontrol(bg,'Style',...
                  'radiobutton',...
                  'String','MATLAB',...
                  'Position',[120 350 200 50]);
                
r2 = uicontrol(bg,'Style','radiobutton',...
                  'String','Python',...
                  'Position',[120 250 200 50]);
  
r3 = uicontrol(bg,'Style','radiobutton',...
                  'String','Javascript',...
                  'Position',[120 150 200 50]);


Output :

Using uibuttongroup(Name, Value)

  • It allows specifying the properties of ButtonGroup by mentioning the Name-Value pair arguments.
  • Some properties are ‘Visible’, ‘Position’, ForegroundColor, etc.

Matlab




% Create a buttonGroup object
bg = uibuttongroup('Visible','on',...
                    "Title","India States",...
                    "TitlePosition","centertop",...
                  "BackgroundColor",'r',...
                  'Position',[0 0 .5 1]);
                
% Create three radio buttons in the button group
r1 = uicontrol(bg,'Style',...
                  'radiobutton',...
                  'String','Andhra Pradesh',...
                  'Position',[50 350 100 30]);
                
r2 = uicontrol(bg,'Style','radiobutton',...
                  'String','Kerala',...
                  'Position',[50 250 100 30]);
  
r3 = uicontrol(bg,'Style','radiobutton',...
                  'String','Assam',...
                  'Position',[50 150 100 30]);
                
% Make the uibuttongroup visible after creating child objects. 
bg.Visible = 'on';


Output :

Using uibuttongroup(parent, Name, Value)

  • It creates buttonGroup object with specified properties in the parent container.
  • The following example displays the button group containing in a figure.

Matlab




% Parent container
fig = uifigure;
  
% Inserting buttonGroup into parent container
bg = uibuttongroup(fig,'Position',[20 20 200 200]);
  
% Adding Toggle buttons in the button group
tb1 = uitogglebutton(bg,'Position',[11 165 140 22],'Text','1');
tb2 = uitogglebutton(bg,'Position',[11 140 140 22],'Text','2');
tb3 = uitogglebutton(bg,'Position',[11 115 140 22],'Text','3');
tb4 = uitogglebutton(bg,'Position',[11 90 140 22],'Text','4');
tb5 = uitogglebutton(bg,'Position',[11 65 140 22],'Text','5');
tb6 = uitogglebutton(bg,'Position',[11 40 140 22],'Text','6');


Output :



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads