Recording a graphics element to a video format
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm having some trouble finding a method to save this graphics animation to a video file. The initial 'array' is a 1200x8 matrix that I'm simply performing interpolation on between points. A .gif file would be the most useful format, but I haven't been successful saving the graphics element 'p' to this file type. Any help is appreciated.
Thanks.
array=-ppdtrace;
array_zeros=[zeros(size(array,1),1) array];
array_zeros=cat(2, zeros(size(array,1),1), array);
x = (0:8)';
xx=0:0.05:8;
yy=makima(x,array_zeros(1,:),xx);
figure(1)
p = plot(x,array_zeros(1,:)',xx,yy);
axis([0 9 -0.85 0]);
for k=2:1200
pause(0.05)
yy=makima(x,array_zeros(k,:),xx);
set(p, 'XData', xx, 'YData', yy);
end
回答(1 个)
Epsilon
2025-2-25
Hi Owen,
To save the generated images into a gif, both ‘exportgraphics’ and ‘imwrite’ functions can be used. The ‘imwrite’ function can be used to write an image to a file location and append the subsequent images to create a gif with a controlled delay. Whereas the ‘exportgraphics’ function (in release R2022a and later) can directly save the graphics object into a gif file.
Attaching a sample code with 12 frames using both the functions:
array = rand(12, 8) * 2 - 1;
array_zeros = [zeros(size(array, 1), 1), array, zeros(size(array, 1), 1)];
x = (0:9)';
xx = 0:0.05:9;
yy = makima(x, array_zeros(1, :), xx);
figure(1)
p = plot(x, array_zeros(1, :)', xx, yy);
axis([0 9 -1 1]);
filename = 'animation.gif';
%Using imwrite function
for k = 1:12
pause(0.05)
yy = makima(x, array_zeros(k, :), xx);
set(p, 'XData', xx, 'YData', yy);
% Capture the current frame
frame = getframe(gcf);
img = frame2im(frame);
[imind, cm] = rgb2ind(img, 256);
% Write to the GIF file
if k == 1
% For the first frame, create the GIF file
imwrite(imind, cm, filename, 'gif', 'Loopcount', inf, 'DelayTime', 0.05);
else
% For subsequent frames, append to the GIF file
imwrite(imind, cm, filename, 'gif', 'WriteMode', 'append', 'DelayTime', 0.05);
end
end
%Using exportgraphics function
for k = 1:12
pause(0.05)
yy = makima(x, array_zeros(k, :), xx);
set(p, 'XData', xx, 'YData', yy);
exportgraphics(gcf,"animation2.gif","Append",true);
end
For further reference on both the functions please refer to the documentation links below:
- imwrite - https://www.mathworks.com/help/matlab/ref/imwrite.html
- exportgraphics - https://www.mathworks.com/help/matlab/ref/exportgraphics.html
Hope it helps.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!