How can i animate the code below?

1 次查看(过去 30 天)
SOUVIK SARKAR
SOUVIK SARKAR 2017-8-18
编辑: MG Poirot 2017-8-18
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 个评论
Akira Agata
Akira Agata 2017-8-18
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 2017-8-18
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.

请先登录,再进行评论。

回答(1 个)

MG Poirot
MG Poirot 2017-8-18
编辑:MG Poirot 2017-8-18
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

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by