How to continue from same line using toggle button gui

3 views (last 30 days)
Here is my test code for toggle button, the 'i' value in for loop gets cleared every time when it returns from toggle state and starts executing from first, How to continue from same point without using counters or manually storing the values
if true
function togglebutton1_Callback(hObject, eventdata, handles)
button_state = get(hObject,'Value');
if button_state == 1
for i=1:10
disp('hi');
pause(1);
else
while(1)
pause(0.1)
end
end
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 15 Nov 2014
Sagar - I think that you want to avoid putting a while loop in your callback, because what will happen is that on subsequent calls to this callback (i.e. the toggling on or off of the button) the callback function will be added to the stack so that you could have multiple togglebutton1_Callback functions "piling up" on top of each other. This will most likely happen when the depressing of the toggle occurs before the 10 iterations of the for loop have completed.
Consider the following code that is based on your function from above
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
toggleState = get(hObject,'Value');
handles.togglebutton1CbackCtr = handles.togglebutton1CbackCtr + 1;
guidata(hObject,handles);
if toggleState
x = handles.togglebutton1CbackCtr;
for k=1:10
fprintf('Callback %d hi (iter=%d)\n',x,k);
pause(1);
end
else
x = handles.togglebutton1CbackCtr;
for k=1:10
fprintf('Callback %d waiting (iter=%d)\n',x,k);
pause(1.0)
end
end
So we are just going to print out the hi statement along with the counter for when that callback fired, along with the iteration of the loop. Note how the while loop has been replaced with a for loop (so that we don't become stuck) has its one print statement for waiting. Now we run the GUI and press the toggle and observe
Callback 1 hi (iter=1)
Callback 1 hi (iter=2)
Callback 1 hi (iter=3)
Callback 1 hi (iter=4)
Callback 1 hi (iter=5)
Callback 1 hi (iter=6)
Callback 1 hi (iter=7)
Callback 1 hi (iter=8)
Callback 1 hi (iter=9)
Callback 1 hi (iter=10)
Which is fine. Now let's try again, and depress the toggle after the third iteration.
Callback 1 hi (iter=1)
Callback 1 hi (iter=2)
Callback 1 hi (iter=3)
Callback 2 waiting (iter=1)
Callback 2 waiting (iter=2)
Callback 2 waiting (iter=3)
Callback 2 waiting (iter=4)
Callback 2 waiting (iter=5)
Callback 2 waiting (iter=6)
Callback 2 waiting (iter=7)
Callback 2 waiting (iter=8)
Callback 2 waiting (iter=9)
Callback 2 waiting (iter=10)
Callback 1 hi (iter=4)
Callback 1 hi (iter=5)
Callback 1 hi (iter=6)
Callback 1 hi (iter=7)
Callback 1 hi (iter=8)
Callback 1 hi (iter=9)
Callback 1 hi (iter=10)
We see that once the second callback has completed "waiting", then the first callback resumes. Now imagine pressing and depressing the toggle multiple 10 times in succession and you would see something like
Callback 1 hi (iter=1)
Callback 2 waiting (iter=1)
Callback 3 hi (iter=1)
Callback 4 waiting (iter=1)
Callback 5 hi (iter=1)
Callback 6 waiting (iter=1)
Callback 7 hi (iter=1)
Callback 8 waiting (iter=1)
Callback 9 hi (iter=1)
Callback 10 waiting (iter=1)
...
Callback 10 waiting (iter=10)
Callback 9 hi (iter=2)
...
Callback 9 hi (iter=10)
Callback 8 waiting (iter=2)
...
Callback 8 waiting (iter=10)
Callback 7 hi (iter=2)
...
Callback 7 hi (iter=10)
Callback 6 waiting (iter=2)
...
Callback 6 waiting (iter=10)
Callback 5 hi (iter=2)
...
Callback 5 hi (iter=10)
Callback 4 waiting (iter=2)
...
Callback 4 waiting (iter=10)
Callback 3 hi (iter=2)
...
Callback 3 hi (iter=10)
Callback 2 waiting (iter=2)
...
Callback 2 waiting (iter=10)
Callback 1 hi (iter=2)
...
Callback 1 hi (iter=10)
Now imagine what would happen if we continued using a while loop to "wait". We would never exit that loop and we would have all the waiting callbacks "stuck" on the stack. You can see this if you call dbstack each time the togglebutton1_Callback is called.
What you may want to do instead, is to use a timer that fires for (say) 10 iterations (once a second) if the toggle button is pressed. If the button is depressed, then we "pause" the timer and allow it to continue once the user presses the toggle again. See the attached GUI which has a "dangerous" toggle (as described above) and a "safe" toggle that uses a timer.

More Answers (1)

Zoltán Csáti
Zoltán Csáti on 15 Nov 2014
Create your loop variable i as a persistent type.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!