Create a movie from matrices

8 次查看(过去 30 天)
Luca Cattaneo
Luca Cattaneo 2024-3-24
评论: DGM 2024-3-24
Hi everyone, I need to create a movie from a series of 50x50 matrices. The real problem is not doing a movie, I succeded doing this using:
v = VideoWriter('peaks.mp4', 'MPEG-4');
open(v);
imagesc(matrix, clims);
frame = getframe;
writeVideo(v,frame);
But how can I save this file as a video? If I download it I can only save the last frame of the movie.
And how to reproduce it in MATLAB? It only shows the movie the first time (and also in a very fast way, but I cannot slow it) and I cannot see it from the beginning another time.
  2 个评论
Stephen23
Stephen23 2024-3-24
Note that GETFRAME essentially captures a screenshot of the displayed axes/figure at the screen resolution. It does not save the image at its native resolution.
But given that you have the image data, you may want to save that (without even needing to display it).
DGM
DGM 2024-3-24
For other ways of directly saving pseudocolor representations of arbitrarily-scaled 2D data without relying on screenshots, see:

请先登录,再进行评论。

回答(1 个)

DGM
DGM 2024-3-24
It only looks like you're capturing one frame, and you're not closing the writer object. There's an example in the documentation.
If you want to adapt that to using imagesc(), that's easy enough:
% some fake data
Z = peaks;
% set up the axes
imagesc(Z,[-10 10]);
axis tight manual
set(gca,"NextPlot","replacechildren")
% open the videowriter object
v = VideoWriter("peaks.avi");
open(v)
% capture all the animation frames
for k = 1:20
imagesc(sin(2*pi*k/20)*Z,[-10 10])
frame = getframe(gcf);
writeVideo(v,frame)
end
% close the writer
close(v);

Community Treasure Hunt

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

Start Hunting!

Translated by