How to create a 2D line plot animation

8 views (last 30 days)
I have a double array of 2001 values which is recorded from SImulink in 20 seconds. When I try to create a video animation of the graph plotting, I get an output of 1:06 minutes. Is there a way to create an animation that would correlate to the original 20 seconds and save it as an mp4 file directly? Here is my code:
load("test1.mat");
video = VideoWriter('test1','MPEG-4');
open(video)
curve = animatedline('Color','r');
set(gca, 'XLim', [0 2001],'YLim', [-200 200])
grid on
for i=1:2001
addpoints(curve,i ,val(i));
drawnow
writeVideo(video,getframe(gcf));
end
close(video);

Accepted Answer

Jimmy Neutron
Jimmy Neutron on 12 Jan 2021
It is a matter of frames per second - as the recording were done at a sample rate of 0.01, the fps was set to 100. Here is te code:
load("test1.mat");
video = VideoWriter('test1','MPEG-4');
video.FrameRate = 100;
open(video)
curve = animatedline('Color','r');
set(gca, 'XLim', [0 20],'YLim', [-200 200])
grid on
for i=1:2001
addpoints(curve,i*0.01 ,val(i));
drawnow
writeVideo(video,getframe(gcf));
end
close(video);

More Answers (0)

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!