Clear Filters
Clear Filters

Using VideoWriter for imagesc

82 views (last 30 days)
Frederik Faye
Frederik Faye on 2 Feb 2016
Commented: Walter Roberson on 19 Sep 2019
I would like to make a movie based on imagesc(M), where M is a matrix that changes with t in a loop. Of course, any other solution that outputs a video file with the frames of what is shown by imagesc would also be fine. Here's what I have:
%looping over t
h = imagesc(M);
axis off
refreshdata(h,'caller')
drawnow
F(t) = getframe(gcf);
%outside loop
v = VideoWriter('newfile.avi','Archival');
v.FrameRate = 60;
open(v)
writeVideo(v,F)
close(v)
However, I get the warning:
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter/close (line 267)
In VideoWriter/delete (line 202)
In Untitled (line 44)
Error using VideoWriter/writeVideo (line 369)
The 'cdata' field of FRAME must not be empty
Error in Untitled (line 47)
writeVideo(v,F)
Thanks!
  1 Comment
Varshini Guddanti
Varshini Guddanti on 7 Jul 2016
Edited: Walter Roberson on 7 Jul 2016
I tried to change the order of your code. Please try this:
% outside loop
v = VideoWriter('newfile.avi','Archival');
v.FrameRate = 60;
open(v)
.
% inside loop
.
%looping over t
h = imagesc(M);
axis off
refreshdata(h,'caller')
drawnow
F(t) = getframe(gcf);
writeVideo(v,F)
% close loop
.
% outside loop
close(v)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 7 Jul 2016
Is it possible that you store one final frame after the end of the t loop, and that final frame is empty? Or alternately, that you store an empty initial frame?
For debugging, add this before the VideoWriter:
all_valid = true;
flen = length(F);
for K = 1 : flen
if isempty(F(K).cdata)
all_valid = false;
fprint('Empty frame occurred at frame #%d of %d\n', K, flen);
end
end
if ~all_valid
error('Did not write movie because of empty frames')
end
  2 Comments
Daniele Bernardo Panaro
Daniele Bernardo Panaro on 19 Sep 2019
Hello I have a similar problem. Using your debugging code (adapting it to my code) it gives me the message
'Empty frame occurred at frame #1 of 1139
Error using UP_MGL_1_2 (line 239)
Did not write movie because of empty frames'
How do I fix the empty frame?
Thank you
Walter Roberson
Walter Roberson on 19 Sep 2019
I notice that only the first frame is empty, not the others. That hints that you might have an off-by-one error in the logic of how you store the frames. Could you show us the code you use to initialize your frame array, and how you update it?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!