How can i animate the code below?

5 ビュー (過去 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 Center および File ExchangeAnimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by