Problem with figure inside a for loop

1 view (last 30 days)
Jules Ray
Jules Ray on 28 Feb 2019
I managed to create a plot to graphically display how displacements at each side of a line (fault) evolve through time if they move at different velocities. I add some reference color lines at the begining and during the loop, so I can see how the dispcacements are accumulated though time. However, when I run the loop they behave quite weird. Everything starts ok until the third reference line is added then the blot become a mess.
Thanks in advance for any idea,
Jules
clear
%create a simple geometry
%time and velocities
dt=20; %dt time
timespan=500;%time in yrs
rate1=0.5; %velocity of area at the left side
rate2=-0.1; %velocity of area at the right side
%time benchmarks for reference lines
B2=[40,200,450];
%Prepare time vector
timebins=0:dt:timespan;
jmax=numel(timebins);
%Initial state
t1=0;
t2=0;
max_fault=max([rate1,rate2])*timespan+30;
min_fault=min([rate1,rate2])*timespan-30;
%the figure
figure(1)
clf
F(jmax) = struct('cdata',[],'colormap',[]);
hold on
plot([0 0],[min_fault max_fault],'-k')%faultor separator between areas that move a diferent velocity
%plot the first reference line
p1=plot([-30 0],[t1 t1],'-b');%sh 0
p2=plot([0 30],[t2 t2],'-b');%sh 0
ylim([min_fault max_fault ])
for j=1:jmax %loop the time
%hold on
time=timebins(j);
%position of terraces
t1=(time*rate1);
t2=(time*rate2);
%Update the 1st reference line
pause(0.8)
set(p1,'ydata',[t1 t1])
set(p2,'ydata',[t2 t2])
%add new reference lines
for i=1:numel(B2) %loop the reference lines
B=B2(i);
clear t3 t4
if time==B %each reference line start at zero
%calculate displacements
t3=(time-B)*rate1;
t4=(time-B)*rate2;
%plot the reference lines
p3=plot([-30 0],[t3 t3],'-');
p4=plot([0 30],[t4 t4],'-');
elseif time>=B %then reference line accumulate displacement through time
%calculate displacements
t3=(time-B)*rate1;
t4=(time-B)*rate2;
%update here the reference lines
set(p3,'ydata',[t3 t3])
set(p4,'ydata',[t4 t4])
drawnow
end
end
%drawnow
%F(j) = getframe(gcf);
end

Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!