Exiting infinite loop on command

10 次查看(过去 30 天)
Alex V
Alex V 2012-4-27
The issue is this: i have an infinite loop defining a short, repeating animation displayed as images in CData of a functionless button called imgwindow. i essentially want to exit it on command, so that the animation stops when the user presses a button. These are the essential code elements:
%imgs loaded in the openfcn
handles.im0 = imread('im0.png');
handles.im1 = imread('im1.png');
handles.im2 = imread('im2.png');
handles.im3 = imread('im3.png');
function startbutton_Callback(hObject, eventdata, handles)
handles.stop = 1;
while handles.stop = 1
for i = 1:3
if i == 1
set(handles.imgwindow,'CData',handles.im1)
elseif i == 2
set(handles.imgwindow,'CData',handles.im2)
elseif i == 3
set(handles.imgwindow,'CData',handles.im3)
end
pause(0.1)
end
end
guidata(hObject,handles);
function stopbutton_Callback(hObject, eventdata, handles)
handles.stop = 2;
set(handles.imgwindow,'CData',im0)
guidata(hObject,handles)
what ends up happening is that im0 does indeed show up, but then the animation resumes. I know that if interrupted in the middle of performing a callback, the original fcn will hold off at a pause, and then resume. I was hoping redefining the stop variable in this pause would then cause it to exit the loop, but that has not been the case. any help? Thanks, Alex

回答(4 个)

TAB
TAB 2012-4-27
Use global variable as a flag to break the loop. Global variables always retains its value belween the function calls.
% --- Executes on button press in Start Button.
function Start_Callback(hObject, eventdata, handles)
global loopFlag;
loopFlag = true;
while true
if(loopFlag==false)
break;
end
disp('Hello');
pause(1);
end
% --- Executes on button press in Stop Button.
function Stop_Callback(hObject, eventdata, handles)
global loopFlag;
loopFlag=false;

Walter Roberson
Walter Roberson 2012-4-27
Just before the end of your "while" loop, inside the loop, add
handles = guidata(hObject);
(You probably don't need the guidata(hObject,handles); call after the loop though.)
  2 个评论
Alex V
Alex V 2012-4-27
this sounds really promising, it makes complete sense really.
i havent tried yet, currently debugging my program (due at noon tomorrow) and want to get it 100% functional before adding bells and whistles type stuff - will update once tried, thanks!
Alex V
Alex V 2012-4-27
and thanks for the source!

请先登录,再进行评论。


Alex V
Alex V 2012-4-27
yeah, turns out that doesn't work, matlab just tells me the stop variable doesn't exist. this is because, as the help documentation says:
DATA = GUIDATA(H) returns previously stored data, or an empty matrix if nothing was previously stored.
and because when the while loop runs, there is no new data for the first many runs, until it is stopped. therefore, i believe all my handles get delorted and matlab just craps the bed.
so what I did was to use 'UserData' which, like TAB's global variable retains its value. unfortunately the loop would still continue through one final time a third of the time, depending on where i interrupted it. so i had to do a bunch of annoying if-else-end business. relevant code is below:
%sets hObject UserData = 1
set(hObject,'UserData',1)
%runs ongoing animation, interrupted by stopbutton setting hObject UserData
%to 0
while get(hObject,'UserData') ~= 0
for i = 1:3
if i == 1
if get(hObject,'UserData') == 0
set(handles.imwind,'CData',handles.im0)
else
set(handles.imwind,'CData',handles.im4n1);
end
elseif i == 2
if get(hObject,'UserData') == 0
set(handles.imwind,'CData',handles.im0)
else
set(handles.imwind,'CData',handles.im4n2);
end
elseif i == 3
if get(hObject,'UserData') == 0
set(handles.imwind,'CData',handles.im0)
else
set(handles.imwind,'CData',handles.im4n3);
end
end
pause(0.1);
end
end

Alex V
Alex V 2012-4-27
thanks for your help, both

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by