Create a movie from matrices

8 views (last 30 days)
Luca Cattaneo
Luca Cattaneo on 24 Mar 2024
Commented: DGM on 24 Mar 2024
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 Comments
Stephen23
Stephen23 on 24 Mar 2024
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 on 24 Mar 2024
For other ways of directly saving pseudocolor representations of arbitrarily-scaled 2D data without relying on screenshots, see:

Sign in to comment.

Answers (1)

DGM
DGM on 24 Mar 2024
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!