saving figures to a video with VideoWriter
46 views (last 30 days)
Show older comments
Eyal Sasson
on 28 Jan 2023
Commented: Eyal Sasson
on 29 Jan 2023
when triyng to plot some data and save the figures to creat video file, i get this error:
i tried to debug the code and coulndnt find a way to fix it.
this is my code:
fs = 250;
t = 0:1/fs:3;
signal_freq = 0.5;
h1 = animatedline('Color','b','Linewidth',1.5);
h2 = animatedline('Color','r','Linewidth',1.5);
y = 180*sin(2*pi*signal_freq*t);
ycos = 180*cos(2*pi*signal_freq*t);
frames = cell(length(t),250/5);
for i= 1:5:length(t)-4
tvec = t(i:i+4);
yvec = y(i:i+4);
ycosvec = ycos(i:i+4);
addpoints(h1,tvec,yvec);
addpoints(h2,tvec,ycosvec);
drawnow;
frames{i} = getframe(gcf);
xlim([0 max(t)+1]);
ylim([-200 200]);
legend("sin","cos");
grid on;
end
obj = VideoWriter("myvideo","MPEG-4");
obj.Quality = 100;
obj.FrameRate = 50;
open(obj);
for i=1:length(frames)
writeVideo(obj,frames{i})
end
obj.close();
0 Comments
Accepted Answer
Walter Roberson
on 28 Jan 2023
getframe() with no options is not guaranteed to return the same size each time. In practice the size of an axes can vary by (if I recall correctly) up to 7 pixels, mostly depending on the labels. You do not xlim or ylim before looping adding points, so the tick locations are being dynamically changed as you go, and that affects the axes width.
More Answers (0)
See Also
Categories
Find more on Audio and Video Data 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!