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

1 vue (au cours des 30 derniers jours)
Sadi Altamimi
Sadi Altamimi le 14 Fév 2013
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.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 14 Fév 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).

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by