Saving animation of projectile motion

18 ビュー (過去 30 日間)
Christo van Rensburg
Christo van Rensburg 2019 年 8 月 6 日
Hi there
I'm struggling to find a way of saving three projectiles on the same graph as a video file. I have three for loops for each iteration that looks like this:
for i1=1:length(Y1(:,1))
addpoints(curve1,Y1(i1,1),Y1(i1,2));
drawnow
pause(0.1)
end
And then the code that actually saves only a piece of the animation so far is:
F(i2) = getframe(gcf);
video = VideoWriter('trajectories.avi', 'MPEG-4');
open(video)
writeVideo(video, F);
close(video)
Please help, it only saves the last trajectory.
Thanks!
  2 件のコメント
darova
darova 2019 年 8 月 7 日
Can you please attach your entire script?
Christo van Rensburg
Christo van Rensburg 2019 年 8 月 7 日
Hi, below is the entire script for the function:
function a = animatevid(Y1, Y2, Y3)
curve1 = animatedline('Color','r','Marker','o');
curve2 = animatedline('Color','b','Marker','^');
curve3 = animatedline('Color','g','Marker','*');
set(gca, 'XLim', [0 100], 'YLim', [-1 8]);
grid on, hold on
xlabel('Distance [m]'), ylabel('Height [m]')
title('Dart Trajectories'),
% --- FOR LOOP FOR ANGLE 1 --- %
for i1=1:length(Y1(:,1))
addpoints(curve1,Y1(i1,1),Y1(i1,2));
drawnow
pause(0.1)
end
pause(0.5)
% --- FOR LOOP FOR ANGLE 2 --- %
for i2=1:length(Y2(:,1))
addpoints(curve2,Y2(i2,1),Y2(i2,2));
drawnow
pause(0.1)
end
pause(0.5)
% --- FOR LOOP FOR ANGLE 3 --- %
for i3=1:length(Y3(:,1))
addpoints(curve3,Y3(i3,1),Y3(i3,2));
drawnow
pause(0.05)
end
legend('Angle 1', 'Angle 2', 'Angle 3')
end

サインインしてコメントする。

回答 (2 件)

KSSV
KSSV 2019 年 8 月 7 日
  1 件のコメント
Christo van Rensburg
Christo van Rensburg 2019 年 8 月 7 日
I can maybe see how this would help, but how do I apply that to three different for loops instead of one major for loop like in script?

サインインしてコメントする。


darova
darova 2019 年 8 月 7 日
Am i correct?
video = VideoWriter('trajectories.avi', 'MPEG-4');
open(video)
for i1 = 1:n1
% draw something
F = getframe;
writeVideo(video, F);
pause(0.02);
end
for i2 = 1:n2
% draw something
F = getframe;
writeVideo(video, F);
pause(0.02);
end
for i3 = 1:n3
% draw something
F = getframe;
writeVideo(video, F);
pause(0.02);
end
close(video)
Or you want three curves drawing simultaneously?
  1 件のコメント
Christo van Rensburg
Christo van Rensburg 2019 年 8 月 7 日
Hi
No the it plots the three trajectories after each other which is fine. I now get the following error and the mp4-file generated doesn't include the graph axis and legend which I assume has something to do with the error message displayed:
Warning: The video's width and height has been padded to be a multiple
of two as required by the H.264 codec.

サインインしてコメントする。

カテゴリ

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