GUI - How to interrupt an endless loop with another callback

1 次查看(过去 30 天)
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!! :)

回答(1 个)

Mr Anderson
Mr Anderson 2016-4-3
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 :)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by