How can I get updated value of UI toggle button, within it's callback function?

2 views (last 30 days)
I want to use a toggle button as an on of switch. So when the button is pressed, the work is done untill the button is depressed. But in my code, the button depress doesn't work because 1st callback (when button pressed) is still being executed, and without the second callback, the Value isn't updated.
function togglebutton1_Callback(hObject, eventdata, handles) % hObject handle to togglebutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of togglebutton1
while (get(hObject, 'Value') == get(hObject,'Max'))
_..do the work.._
end

Accepted Answer

Jan
Jan on 28 Apr 2015
If you set the 'Interruptible' property to 'off' and allow Matlab to react to actions using drawnow, the button should change its value.

More Answers (1)

Stephen23
Stephen23 on 28 Apr 2015
Edited: Stephen23 on 28 Apr 2015
I also wanted to use a toggle-button as an on-off switch for some looped operation, and solved this using some fairly simple code. The core of it is a loop like this:
function chvDemo(tgh,~)
% ...some other code
while ishghandle(tgh) && get(tgh,'Value')
% code to run while switch is "on"
end
end
where the button's value is either one or zero:
uicB(2) = uicontrol(figH, 'Style','togglebutton', 'Units','normalized', 'Max',1, 'Min',0, 'Callback',@chvDemo);
and the function chvDemo contains that while-loop above. And that is it! You can see the whole code and try it for yourself too: check out the function cubehelix_view that you can find here:

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!