how to set togglebutton value from another togglebutton
1 次查看(过去 30 天)
显示 更早的评论
Hello,
i have two togglebuttons in my gui which i created from guide.
i want when i press the first one then automatically pressed and the second one.
in the first toggle button i set:
set(handles.togglebutton2, 'Value', 1);
The togglebutton2 seems to be pressed,however the code in the togglebutton2 doesn't execute.
Any idea what to do to execute the code in the togglebutton2 when i press the first one?
0 个评论
采纳的回答
Walter Roberson
2016-3-6
You cannot get the second toggle to run just by setting its Value. You need to call its callback. If you constructed your GUI using GUIDE, that might look something like
evalin('base', get(handles.togglebutton2, 'Callback'))
but you could probably instead call
togglebutton2_Callback(handles.togglebutton2, event, handles)
where "event" should be named the same thing as the second parameter to the callback you are executing at the time.
3 个评论
Walter Roberson
2016-3-6
I have tested and confirmed that any remaining code in the togglebutton1_Callback will be executed after calling togglebutton2_Callback .
If you are setting variables in togglebutton2_Callback and expecting the variables to be available in togglebutton1_Callback, then remember that the two functions have distinct workspaces and that variables set in one will not appear in the other.
If you are setting something in the handles structure in togglebutton2_Callback and expecting the changed value to be available in togglebutton1_Callback, then you need to have used guidata() to update the "master" copy of the handles structure, and then after you call to togglebutton2_Callback, you need to call guidata() in togglebutton1_Callback to fetch the updated copy of "handles".
更多回答(1 个)
Helmut Riedel
2017-3-22
Hi Alex,
I implemented a function togglegroup.m that can toggle 5 togglebuttons with a master togglebutton. Interestingly, the actual callback function has not to do anything, since the button states (and their changes/toggles) are read and set in the main function.
Hope that helps.
Cheers,
Helmut
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!