GUI - How to interrupt an endless loop with another callback

3 views (last 30 days)
Hello, I have some truble interrupting an animation in a GUI I wrote. The loop gets called by some Button1 like this
sd = 1;
loop_running = true;
KW=0;
axes(handles.axes1)
while loop_running
if KW+sd>size(KnM,3)
sd=-1;
elseif KW+sd==0
sd=1;
end;
KW = KW+sd;
scatter3(scatter information);
axis([0 0.2 -0.1 0.1 -0.1 0.1])
handles.KW = KW;
set(handles.edit8,'String',num2str(KW));
guidata(hObject,handles);
pause(PauseTime);
if isfield(handles,'Pause')
if handles.Pause
break;
end;
end;
%loop_running=handles.loop_running;
end;
Then there is my Pause Button
global test
if isfield(handles,'Pause')
if handles.Pause
handles.Pause = false;
else handles.Pause
handles.Pause=true;
end;
else
handles.Pause = true;
end;
test=1;
guidata(hObject,handles);
Button1 is interruptible, so when PauseButton is clicked, at pause(PauseTime); the PauseButton script runs, creates the handles.Pause field, BUT gets kinda deleted right afterwards, so the if isfield(handles,'Pause') is always false. I also tried to set loop_running=false with the same result. Its just like the callback of PauseButton never happens even though when debugging I can see the handles.Pause is created, but just as well beeing deleted right afterwards(same with that global variable I tested). The other answers I found, had a similar scipt or way to approach interrupting a loop.
Can somebody tell me what Im missing?
Many thanks in advance!! :)

Answers (1)

Mr Anderson
Mr Anderson on 3 Apr 2016
UPDATE:
I just forgot to set the global test variable in my Button1 Callback, so the break using a global variable works fine.
But since global variables are bad, I'd still appreciate any help to use the handles Struct, or telling me what i did wrong.
Thanks :)

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!