I have a problem with 'togglebutton' in MATLAB 2012a GUI

조회 수: 1 (최근 30일)
Sadi Altamimi
Sadi Altamimi 2013년 2월 14일
Hello everyone;
Let us say we have a text file that contains a number. we need to continuously read the file and print that number on the screen. (Obviously we will need some kind of loop)
Note that reading process should start only when a toggle button is pressed, and stop once the button is pressed again (i.e. released).
My problem is that every time I run the program, it fall in an infinite loop! I assumed that the function will monitor the button state since I include the 'get' function in the loop.
Here is the code: (I have replaced the reading commands with a simple addition statement to simplify the work)
function tbtn1_Callback(hObject,eventData)
%
isPressed = get(hObject,'Value'); % read the button state
if isPressed
set(hObject,'String','Stop');
else
set(hObject,'String','Start');
end
ii = 0;
while isPressed
ii = ii+1 %instead of reading the text file
isPressed = get(hObject,'Value'); % check if the button released
end
end
Hope you can help! Thanks in advanced.

채택된 답변

Sean de Wolski
Sean de Wolski 2013년 2월 14일
Put a drawnow inside of the while-loop before you get(hObject,'Value').
This forces MATLAB to flush the event queue before continuing the loop which may have some queued up events (i.e. pushing the button).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by