Saving animation of projectile motion

조회 수: 18 (최근 30일)
Christo van Rensburg
Christo van Rensburg 2019년 8월 6일
댓글: Christo van Rensburg 2019년 8월 7일
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 CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by