File Exchange

image thumbnail

ButtonGroup Objects

version (5.66 KB) by Matthew Whitaker
Container objects to manage radio or toggle buttons with functionality of the GUI Layout Toolbox


Updated 21 Jun 2011

View License

The ButtonGroup controls require the GUI Layout Toolbox by Ben Tordoff and David
Sampson available at:
The controls in this submissionshould be placed in the +uiextras folder in the GUI Layout Toolbox or in another folder called +uiextras that resides under a folder on your path. It is recommended to run rehash path after doing this.

function buttonGroupExample
f = figure;
vb = uiextras.VBox('Parent',f,'Units','Normalized');
bgH = uiextras.HButtonGroup('Parent',vb,'Buttons',{'1','2','3'},'Spacing',50,'Padding',10,'SelectedChild',1,'SelectionChangeFcn',@onSelectionChange);
hb = uiextras.HBox('Parent',vb);
bgV = uiextras.VButtonGroup('Parent',hb,'ButtonStyle','toggle','Buttons',('123')','Spacing',20,'Padding',10,'SelectedChild',3,'SelectionChangeFcn',@onSelectionChange);
p = uiextras.Panel('Parent',hb);
lblDisplay = uicontrol('Parent',p,'FontSize',16,'Style','text');
function onSelectionChange(src,evt)
end %onSelectionChange
end %buttonGroupExample

Cite As

Matthew Whitaker (2020). ButtonGroup Objects (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Clayton Chu

The proposed corrections for R2014b compatibility are a good start, but more corrections need to be done. Right now, my radio buttons act as toggles, which is quite odd. Any idea on how to fix this???



Hi there,

Here some corrections to do for R2014b.
In the classe ButtonGroup, objet.GroupHandles could be empty in release R2014b. Maybe, there are others maners to do it, but here one :

So since line 81, you can replace following reference about "obj.GroupHandles" by :

gChildObj = allchild(obj);
numChild = numel(gChildObj); % OLD numel(obj.GroupHandles);
if val > numChild;
'The selected child value %d exceeds the number of controlled children: %d',...
end %if

groupIdx = 1:numChild;
if ~isempty(groupIdx)
groupIdx(val) = [];
if ~isempty(groupIdx)
% OLD set(obj.GroupHandles(groupIdx),'Value',0,'Enable','on');
end %if
% OLD set(obj.GroupHandles(val),'Value',1,'Enable','inactive');
obj.SelectedChild_ = val;
end %if

Clayton Chu

Is there a way you could extend this capability to do something like uiextras.ButtonGrid? This way we could lay out radio buttons in a grid fashion. Great work!


Hi again,

fixed it by replacing line 50 in HButtonGroup and also VButtonGroup

if isa(evt.Child,'uicontrol')

with this line:

if ishandle(evt.Child)

So, only check for a valid graphics object handle instead of checking for class 'uicontrol'. I think this should be sufficient.
Obviously MATLAB's CLASS function returns 'double' in this case and hence the added object is not controlled by the ButtonGroup leading to the error described in my previous posting.

Hope this helps someone else...



I installed this files in the +uiextras folder. I try to run your example.
f = figure;
vb = uiextras.VBox('Parent',f,'Units','Normalized');
bgH = uiextras.HButtonGroup('Parent',vb,'Buttons',{'1','2','3'},'Spacing',50,'Padding',10,'SelectedChild',1,'SelectionChangeFcn',@onSelectionChange);

After the third line I get an error:
Undefined variable "uiextras" or class "uiextras.ButtonGroup".

I have Gui Toolbox running correctly. Any Ideas? thanks

Henry Harrison

Thanks for this, Matthew. One question though. There may be an issue with maintaining the 'Enable' setting of the children. In my GUI I have a toolbar, and some tools are only available in certain situations. Setting the children's 'Enable' state works fine, until I select a tool, at which point all disabed buttons become enabled.

There is an easy workaround, of course, to re-disable the disabled buttons at the end of the callback function. However I though I would bring the issue to your attention.

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: GUI Layout Toolbox