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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by