Saving animation of projectile motion

18 次查看(过去 30 天)
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
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
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
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 CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by