如何在 MATLAB 中添加 ButtonGroup ?
ButtonGroup允许用户管理切换按钮和单选按钮 图中。 ButtonGroup 对象可以使用 uibuttongroup() 方法创建。
uibuttongroup() 的不同语法是
- bg = uibuttongroup()
- bg = uibuttongroup(名称,值)
- bg = uibuttongroup(parent,Name,Value)
现在,让我们详细讨论上述语法:
使用 uibuttongroup()
- 它在当前图形中创建一个按钮组并返回buttonGroup对象。
- 在以下示例中,单选按钮插入按钮组中。
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]);
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';
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');
输出 :
使用 uibuttongroup(Name, Value)
- 它允许通过提及名称-值对参数来指定 ButtonGroup 的属性。
- 一些属性是“可见”、“位置”、前景颜色等。
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';
输出 :
使用 uibuttongroup(parent, Name, Value)
- 它创建具有指定属性的buttonGroup 对象 父容器。
- 下面的示例显示图中包含的按钮组。
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');
输出 :