How to plot different iterations with a time delay in for loop?

4 views (last 30 days)
Hi,
I am having some trouble plotting different iterations with a time delay of for example one second in my loop:
load Data_SATP.mat
Days_SATP = Data_SATP([1:101:3131],[2]);
L_ATP = Data_SATP([2:101],[1]);
for j = 1:31
Sw = Data_SATP([(2+(101*(j-1))):(101*j)],[2]);
mat(j,:)=Sw
end
figure(2)
for k=(1:31);
plot(L_ATP,mat((1:31),:))
end
after which I fix the labels and axis. Now I get one plot of 31 graphs, but I would like to make the graphs appear one after each other in the same plot, preferably as an animated plot, but otherwise at least in the order of graph1, 1 second later graph2, etc.. I tried with pause(1), and I got it working once but after that I couldn't get it to work again after I changed some things, so advice on that is also welcome.
Thanks
[EDITED, Jan, Code formatted]

Answers (1)

Jan
Jan on 29 Nov 2015
The body of the FOR loop over k does not depend ob k at all. I guess, that you want mat(k, :):
H = plot(L_ATP,mat(1, :))
for k = 2:31
pause(0.5);
set(H, 'YData', mat(k, :));
end
  1 Comment
MT
MT on 29 Nov 2015
Edited: MT on 2 Dec 2015
I have another question related to this, I used the advice you gave me and now a part of it consists of this:
clc;
close all;
clear all;
load CS1.mat
Days_SATP= CS1([1:21:189],[2]);
L_ATP= CS1([2:21],[1]);
for j=(1:9);
Sw= CS1([(2+(21*(j-1))):(21*j)],[2]);
mat(j,:)=Sw;
end
figure;
H = plot(L_ATP,mat(1, :));
title('Aqueous phase')
xlabel('Length (-)')
ylabel('Saturation')
axis([0 1 0 1])
for k = 2:9
pause(0.5);
set(H, 'YData', mat(k, :));
end
load CS2.mat
for j=(1:9);
Sw= CS2([(2+(21*(j-1))):(21*j)],[2]);
mat(j,:)=Sw;
end
figure;
H = plot(L_ATP,mat(1, :));
title('Aqueous phase')
xlabel('Length (-)')
ylabel('Saturation')
axis([0 1 0 1])
for k = 2:9
pause(0.5);
set(H, 'YData', mat(k, :));
end
which gives me two plots after each other for a different data set. How can I plot both the data of CS1 and CS2 simultaneously in one figure?
I tried the following, with different tries for the set(...) part, but I can't figure it out:
clc;
close all;
clear all;
load CS1.mat
load CS2.mat
Days_SATP= CS1([1:21:189],[2]);
L_ATP= CS1([2:21],[1]);
for j=(1:9);
Sw= CS1([(2+(21*(j-1))):(21*j)],[2]);
mat(j,:)=Sw;
Sw2= CS2([(2+(21*(j-1))):(21*j)],[2]);
mat2(j,:)=Sw2;
end
figure;
H = plot(L_ATP,mat(1, :));
H2 = plot(L_ATP,mat2(1, :));
title('Aqueous phase')
xlabel('Length (-)')
ylabel('Saturation')
axis([0 1 0 1])
for k = 2:9
pause(0.5);
set(H, 'YData', mat(k, :),H2,'YData', mat2(k,:));
end
Regards,
Martin

Sign in to comment.

Categories

Find more on Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!