My program runs a function when user clicks on an axes object. This function uses the position of cursor and shows its progress as an animation. What I need is to stop currently running call of function when user clicks a new position, and then call the function for this new position.
My code is something like this (in my original code I use guidata
and handles
instead of global variables):
function TestUI
clc; clear variables; close all;
figure; axis equal; hold on;
xlim([0 100]); ylim([0 100]);
set(gca, 'ButtonDownFcn', @AxisButtonDownFcn);
global AnimateIsRunning
AnimateIsRunning = false;
end
function AxisButtonDownFcn(ah, ~)
C = get(gca,'CurrentPoint');
global xnow ynow AnimateIsRunning
xnow = C(1, 1); ynow = C(1, 2);
if AnimateIsRunning
% ---> I need to wait for termination of currently running Animate
end;
Animate(ah, xnow, ynow);
end
function Animate(ah, x, y)
T = -pi:0.02:pi; r = 5;
global xnow ynow AnimateIsRunning
AnimateIsRunning = true;
for t = T
if ~((xnow==x)&&(ynow==y))
return;
end;
ph = plot(ah, x+r*cos(t), y+r*sin(t), '.');
drawnow;
delete(ph)
end
AnimateIsRunning = false;
end
My problem is that any newer clicks interrupt currently running function and keeps previous running Animate
in a stack. It makes the last drawing of the previous animation remain visible. The worse is that the size of the stack seems to be 8 and newer interruptions will be stored in a queue! Meaning user can update position only 8 times. To see the problem you can run the code sample above and click on the axes object repeatedly.
Now, I want to check if Animate
is running in AxisButtonDownFcn
, and wait for its termination (or terminate it by force), and then call Animate
with new parameters.