Help - Nested uitogglebutton State Change Callback

In matlab i have a nested uitogglebutton object. like this: fig > uipanel > uigridlayout > uibuttongroup > uitogglebutton. Because the toggle button is contained within uibuttongroup, uitogglebutton callabacks are not allowed. However, uibuttongroup SelectionChangedFcn fires callback if different button within buttongroup is selected and not toggle button state change. (Note: uicontrol with togglebutton not allowed when using uigridlayout). So, how do I detect a uitogglebutton state change and perform callback? Appreciate any help you can provide!
PseudoCode:
fig = figure(...)
panel = uipanel(fig, ...)
grid = uigridlayout(panel, ...)
btngrp = uibuttongroup(grid, ..., 'SelectionChangedFcn',selectionFcn)
tglbtn = uitogglebutton(btngrp, ...) %% callback not allowed on state change
function selectionFcn(src, event) %% Function not executed if togglebutton state change
selectedBtn - src.SelectedObject
if selectedBtn.Value == 1
DoSomething
else
DoSomethingElse
end
end

 Accepted Answer

Umar
Umar 9 minutes ago
Edited: Umar 8 minutes ago
Hi @Keith,
This isn't something you're missing in your code, it's a hard limitation of how uitogglebutton and uibuttongroup are designed to work together, and it's confirmed by MathWorks' own documentation.
Here is why the callback never fires. First, uitogglebutton doesn't have a state change callback at all. Its only callback properties are CreateFcn and DeleteFcn, there's no ValueChangedFcn, unlike uibutton or uiswitch. Second, a uitogglebutton's Parent is required to be a ButtonGroup object, so you can't parent it straight to a uigridlayout the way you can with a plain uibutton. Third, uibuttongroup only exposes SelectionChangedFcn, which fires when SelectedObject changes, meaning when a different button in the group becomes selected. A button group is built to always keep exactly one button selected, like radio buttons, so re-clicking the same or only button isn't treated as a change at all.
So in short, uibuttongroup plus uitogglebutton is a mutual exclusion "pick one of several" widget, not an independent on and off switch, which is exactly why there's no event to catch when a single toggle flips its own state.
The workaround is this. If you want independent on and off behavior, skip uitogglebutton and use a plain uibutton instead. It fires ButtonPushedFcn on every click and parents directly to a uigridlayout with no button group needed. You track pressed and unpressed state yourself using Text, Icon, or BackgroundColor.
I've attached a PDF that walks through this same explanation in more detail and includes a full working code sample with comments explaining the reasoning, along with links to the exact MathWorks documentation pages this is based on, covering ToggleButton properties, uitogglebutton, uibuttongroup, and uibutton.
You can open the attachment for the complete code you can copy straight into your project.
Hope this helps!

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 23 Jul 2026 at 17:09

Edited:

about 15 hours ago

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!