How to call a UIButtonGroup SelectionChangeFunction

9 次查看(过去 30 天)
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

回答(2 个)

Image Analyst
Image Analyst 2014-1-2
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 个评论
Dan
Dan 2014-1-2
I thought about that & it would work had I not tied the button's state to the event data. I hesitate to explain the whole story, but suffice it to say that I was using a bunch of radio buttons but wanted something more pretty and so put some buttons in this group, slapped picture on the buttons, & now alternately turn each button "On" and "Off" like this:
%Get button states sOldButtonSelectionTag=get(eventdata.OldValue,'tag'); sNewButtonSelectionTag=get(eventdata.NewValue,'tag');
%Uset the old button; set(eventdata.OldValue,'string',''); %Set the new button; set(eventdata.NewValue,'string','ON');
But... if I stick with this paradigm & change the string property outside the SelectionChangeFunction it will mix things up (the sOldButtonSelectionTag will no longer be the one that is "ON" and so it won't be set off & I'll end up with two buttons "ON" when I want them to be mutually exclusive). I guess I'm creating a palette of tools from which only one may be selected at a time.
So... I *could just kludge something together & scan all the strings of all the buttons and.... yuch. So it would solve everything if I could just simulate the "pressing" of a button. Something like :
eventdata.EventName='SelectionChanged';
eventdata.OldValue=handles.tbAddROI;
eventdata.NewValue=handles.tbPointer;
uipanelMouseFunctions_SelectionChangeFcn(hObject, eventdata, handles);
But this essentially creates the same confusion I described with your suggestion.
I think I need to use "notify" and trigger an event but before heading down that rabbit hole I thought I would just ask for suggestions.
Dan
Dan 2014-1-2
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....

请先登录,再进行评论。


David Stapleton
David Stapleton 2016-2-15
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'.

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by