MATLAB Answers

How to call a UIButtonGroup SelectionChangeFunction

7 views (last 30 days)
Dan
Dan on 2 Jan 2014
Answered: David Stapleton on 15 Feb 2016
Hi! I have a few toggle buttons in a UIButtonGroup and all is well. However, in some circumstances I want to change the state of the buttons programmatically and I naively thought I could do it by simply calling the "SelectionChangeFunction" function with an appropriately defined eventdata structure. This almost works. The function does what I want but I guess I confused the UIButtonGroup in some way because subsequent clicks on buttons make it behave strangely. It basically doesn't realize that I have programmatically clicked on a button and "remembers the "old state". I tried setting the "SelectedObject" to my desired button (hoping that would automagically call the function) but.... nuttin.
Do I need to be "triggering an event"? I'm not sure how to do this.... Advice?
Dan

Answers (2)

Image Analyst
Image Analyst on 2 Jan 2014
Just have the button group callback call another function called ButtonClicked.
function handles = ButtonClicked(handles)
% Now do whatever you want.
Now that will get called when the user clicks on a button, plus you can also call it whenever you want from any function at all.
  2 Comments
Dan
Dan on 2 Jan 2014
In the end, I just used a sleazy workaround with a persistent variable to remember the button states.
But thanks for listening to my ramblings....

Sign in to comment.


David Stapleton
David Stapleton on 15 Feb 2016
I was having the same problem and stumbled into a simple workaround. I'm not sure if this solution is bug-free, but it's working for me. Basically combine the two methods you started with, calling the function with spoofed eventdata and immediately resetting the SelectedObject so that it matches. You can reset before or after you call the function, as long as you pulled the previous value to use for the OldValue in the spoofed eventdata.
eventdata.OldValue=Group.SelectedObject;
eventdata.NewValue=Option2;
eventdata.Source=Group;
eventdata.EventName='SelectionChanged';
Group.SelectedObject=Option2;
SelCngd(Group,eventdata);
As far as I can tell, calling the function gets you all of the results of an actual event while ignoring SelectedObject and manually changing that doesn't have any follow-up effects.
I also tried to use notify() but all I got was: No method 'notify' with matching signature found for class 'matlab.ui.container.ButtonGroup'.

Community Treasure Hunt

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

Start Hunting!

Translated by