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

8 vues (au cours des 30 derniers jours)
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

Réponse acceptée

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

Plus de réponses (1)

Stephen23
Stephen23 le 28 Avr 2015
Modifié(e) : Stephen23 le 28 Avr 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:

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by