MATLAB: Problem: how to stop animation in GUI and problem with stacking plots in axes

animationaxesdrawnowguiplotsrefreshdatastartstop

heya, i found a lot of materials how to solve my problem, but simply i couldnt use it properly, bcs of that i'm asking you
So as Title says, I want to stop my animation in gui, since it is pretty long, and simply i don't want to use "CTRL-C" to terminate animation
Introduction: i want to have 2 buttons, one for start animation, and one for stop animation, howevere i dont know how to programm that stop animation button
this is my code for start animation btn:
axes(handles.axes3)
title('Uskladištenje tekućine u 2 spremnika','color',[0.6 0 0]);
xlabel('Širina Spremnika [m]');
ylabel('Razina tekućine [m]');
z1=2*(sqrt((evalin('base','A1'))/(evalin('base','pi'))));
z2=2*(sqrt((evalin('base','A2'))/(evalin('base','pi'))));
line([0.1 0.1], [0 6]);
line([0.1+z1 0.1+z1], [0.4 6]);
line([0.1+z1 0.1+z1+0.2], [0.4 0.4]);
line([0.1+z1+0.2 0.1+z1+0.2], [0.4 6]);
line([0.1+z1+0.2+z2 0.1+z1+0.2+z2], [0.25 6]);
line([0.1+z1+0.2+z2 0.1+z1+0.2+z2+0.2], [0.25 0.25]);
line([0.1 0.1+z1+0.2+z2+0.2], [0 0]);
%slavina

line([0 0.4], [6.75 6.75]);
line([0 0.35], [6.5 6.5]);
line([0.35 0.35], [6.5 6.3]);
line([0.4 0.4], [6.75 6.3]);
hold on
%slavina
area([0 0.385], [6.7 6.7], 6.55);
area([0.365 0.385], [6.55 6.55], 6.25);
%
area([z1+0.1 z1+0.1+0.2], [0.4 0.4]);
area([z1+0.1+0.2+z2 0.1+z1+0.2+z2+0.2], [0.25 0.25]);
vel=length(evalin('base','h1'));
h1=evalin('base','h1');
h2=evalin('base','h2');
h10=evalin('base','h10');
h20=evalin('base','h20');
x1=[0.1 0.1+z1];
y1=[h10 h10];
x2=[0.1+z1+0.2 z1+0.1+0.2+z2];
y2=[h20 h20];
s1=area(x1,y1);
s2=area(x2,y2);
for j=1:vel
set(s1,'Ydata',[h1(j) h1(j)])
set(s2,'Ydata',[h2(j) h2(j)])
pause(.1)
refreshdata
drawnow
end
and now i just want somehow to embedd to GUI stop button which will stop that for loop, bcs it is used for animation.
Also i have one more question, as you can see, this button will draw few lines, and areas, but when i change parameters of model(which is animated), lines and areas will change, but in axes will stay previous "image".
Also i thought its problem with hold on but if i remove it than lines won't stay in axes, so if you know how to solve this problem, ill appreciate it.
Also if you need more information, just type it here
Thank you in advance!

Best Answer

Add an if-statement in your for-loop.
if get(handles.pbStop,'Value')
% do all those stuff
else
break; %terminate the for-loop
end