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

1 view (last 30 days)
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.

Accepted Answer

Sean de Wolski
Sean de Wolski on 14 Feb 2013
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).

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!