How to generate video file from animated plot in Matlab?

Dear scholars,
I have a problem with generating .avi video file from animated file. I attach the code below, I appreciate if sb can give me a hand in figuring out the problem.
It generates the vidoe but the video is quite short and practically does NOT show anything!
Regards
x = linspace(0, 1*pi, 5000)
y = exp(-0.05.*100.*x).*sin(100.*x+0);
curve = animatedline('Color', 'k', 'LineStyle', '-', 'LineWidth', 2)
set(gca, 'XLim', [0 1*pi]);
box on
for i = 1 : length(x)
addpoints(curve, x(i), y(i));
drawnow
end
legend('Classical Rod Model', 'FontSize', 12)
xlabel('t (s)', 'FontSize', 12)
ylabel('Amplitude', 'FontSize', 12)
obj = VideoWriter('curve.avi');
obj.Quality = 100;
obj.FrameRate = 3;
open(obj);
f = getframe(gcf)
writeVideo(obj, f)
obj.close();

 Accepted Answer

It is because, you are writitng only the last frame into video...you need to keep the video writer code into a loop.
x = linspace(0, 1*pi, 1000) ;
y = exp(-0.05.*100.*x).*sin(100.*x+0);
curve = animatedline('Color', 'k', 'LineStyle', '-', 'LineWidth', 2) ;
set(gca, 'XLim', [0 1*pi]);
box on
f = cell(length(x),1) ;
for i = 1 : length(x)
addpoints(curve, x(i), y(i));
drawnow
f{i} = getframe(gcf) ;
end
legend('Classical Rod Model', 'FontSize', 12)
xlabel('t (s)', 'FontSize', 12)
ylabel('Amplitude', 'FontSize', 12)
obj = VideoWriter('curve.avi');
obj.Quality = 100;
obj.FrameRate = 3;
open(obj);
for i = 1:length(x)
writeVideo(obj, f{i}) ;
end
obj.close();

4 Comments

Thanks a lot for the point. I appreciate your great help.
Do you know how to save the generated moive (animation) in 'MPEG' format? I use ('filename', 'MPEG-4') bit still does not work. Here is the code:
x = linspace(0, (1)*pi, 500) ;
y = exp(-0.05.*100.*x).*sin(100.*x+0);
curve = animatedline('Color', 'k', 'LineStyle', '-', 'LineWidth', 2) ;
set(gca, 'XLim', [0 1*pi]);
box on
f = cell(length(x),1) ;
for i = 1 : length(x)
addpoints(curve, x(i), y(i));
drawnow
f{i} = getframe(gcf) ;
end
legend('Classical Rod Model', 'FontSize', 12)
xlabel('t (s)', 'FontSize', 12)
ylabel('Amplitude', 'FontSize', 12)
obj = VideoWriter('curve.avi');
obj.Quality = 100;
obj.FrameRate = 20;
open(obj);
for i = 1:length(x)
writeVideo(obj, f{i}) ;
end
% objmp = VideoWriter('curve.mp4' , 'MPEG-4');
objmp = VideoWriter('curve.mp4' , 'Motion JPEG 2000');
%objmp.Quality = 100;
v.Quality = 100;
objmp.FrameRate = 20;
open(objmp);
for i = 1:length(x)
writeVideo(objmp, f{i}) ;
end
obj.close();
Try changing the last line of your code to
objmp.close();
Thanks for such a great help.
I was wondering how to create multiple animations in a single frame?
(just like plotting multiple pictures in a single plot using the subplot)
Is there sth similar?
Thanks!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!