How can i animate the code below?

1 view (last 30 days)
SOUVIK SARKAR
SOUVIK SARKAR on 18 Aug 2017
Edited: MG Poirot on 18 Aug 2017
t1=0:0.01:1;
i=0;
m1=0;
while(i==0)
for i=1:99
y1(i)=1.*t1(i);
end
for i= 100:200
y1(i)=1;
end
y=[zeros(1,200+m1) y1 zeros(1,801-m1)];
%y=y1+y2;
t2=-2:0.01:10;
plot(t,y)
ylim([0 2]);
xlim([-2 10]);
m1=m1+200;
j=input('enter 0 4 times');
end
  2 Comments
Akira Agata
Akira Agata on 18 Aug 2017
Could you reshape your code to "easy-to-read" style? Trying copy&past your code to MATLAB, but it does not work.
Geoff Hayes
Geoff Hayes on 18 Aug 2017
Souvik - what are you trying to animate? When plot is called, you are passing an invalid parameter t (which doesn't exist). Even if I replace it with t1, it is of a different dimension than y. Please clarify what you are trying to do.

Sign in to comment.

Answers (1)

MG Poirot
MG Poirot on 18 Aug 2017
Edited: MG Poirot on 18 Aug 2017
If I'm interpreting your question corretly (and fixed your code properly) I faced the same problem years ago: animating plots withing a for/while loop.
Back then I found a solution with two options. The solution is to call Matlab the draw the figure before the next loop, this can be done either by the drawnow command or the pause(s) command, if you would like to lower the drawspeed.
Your script would look like:
t1=0:0.01:1;
i=0;
m1=0;
while(i==0)
for i=1:99
y1(i)=1.*t1(i);
end
for i= 100:200
y1(i)=1;
end
y=[zeros(1,200+m1) y1 zeros(1,801-m1)];
%y=y1+y2;
t2=-2:0.01:10;
plot(t2,y)
ylim([0 2]);
xlim([-2 10]);
m1=m1+200;
%drawnow
pause(0.1)
i = 0
end
Resulting in the animation of a forward propagating wave. I love to know what you think and solution addresed your question properly.
-mgp

Categories

Find more on Animation 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!