Lemmy
Lemmy

Reputation: 319

How to stop figure repeatedly popping up after closing animation

Lately I've started to make a crank mechanism animation. The animation works well, but when I tried to close the Figure 1 window it just keeps popping up.

I don't know how to stop this?

Code:

clc;
clear all;
radius=2;
vzdOdKliky=6;
bod1=[0,0];
axis(gca, 'equal');
mezera=[-4,8,-4,8];
axis(mezera);
speed=1;
for time=1:200
    theta=speed*(time/10);
    bod2=radius*[cos(theta),sin(theta)];
    alfa=asin(radius*sin(theta)/vzdOdKliky);
    bod3=[(radius*cos(theta)+vzdOdKliky*cos(alfa)) 0];
    klika=line([bod1(1),bod2(1)],[bod1(2),bod2(2)]);
    klouzM=line([bod2(1),bod3(1)],[bod2(2),bod3(2)]);
    trajB2=viscircles([0,0],radius,'LineStyle',':');
    kruhB1=viscircles(bod1,0.3);
    kruhB2=viscircles(bod2,0.3);
    kruhB3=viscircles(bod3,0.2);
    pause(0.001);
    delete(klika);
    delete(kruhB1);
    delete(kruhB2);
    delete(kruhB3);
    delete(klouzM);
end

Upvotes: 2

Views: 181

Answers (1)

Wolfie
Wolfie

Reputation: 30121

Your loop continues regardless of whether you've closed the figure or not.

Include a check on the figure's existence each loop like so:

% Open figure and store to variable for checking later
fg = figure;
% ... your setup code ...
for time = 1:200
    % Check if the figure still exists
    if ~isvalid(fg)
        % Exit looping, figure has been closed
        break
    end
    % ... other code in the loop ...
end

If you wanted to be less elegant, you could always hit Ctrl+C to terminate your script.

Upvotes: 7

Related Questions