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

조회 수: 6 (최근 30일)
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

채택된 답변

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

추가 답변 (1개)

Stephen23
Stephen23 2015년 4월 28일
편집: Stephen23 2015년 4월 28일
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:

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by