Saving animation of projectile motion

15 views (last 30 days)
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 Comments
darova
darova on 7 Aug 2019
Can you please attach your entire script?
Christo van Rensburg
Christo van Rensburg on 7 Aug 2019
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

Sign in to comment.

Answers (2)

KSSV
KSSV on 7 Aug 2019
  1 Comment
Christo van Rensburg
Christo van Rensburg on 7 Aug 2019
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?

Sign in to comment.


darova
darova on 7 Aug 2019
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 Comment
Christo van Rensburg
Christo van Rensburg on 7 Aug 2019
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.

Sign in to comment.

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!