MATLAB: Weird bug in getframe

animationgetframeMATLABmovievideovideowriter

Hello!
I have encountered a very weird behaviour of the getframe function. I essentially create a new frame in a for loop by
figure(10)
image(xvekt*1e6,yvekt*1e6,I2/max(max(I2))*64)
hold on
plot(D_core/2*cos(linspace(0,2*pi,50))*1e6,D_core/2*sin(linspace(0,2*pi,50))*1e6,'Color',[1 1 1]*0.6,'LineWidth',2)
colormap(jet)
hold off
zoom(5)
drawnow
pause(.1)
F(steg_nummer) = getframe;
Exactly what the code does is not very important, but notice the pause(0.1) before the getframe call to make sure getframe really captures the frame. steg_nummer is the iteration variable.
After the for loop, I attempt to save the animation by
v = VideoWriter('C:\newfile.avi');
open(v);
for k=1:length(F)
k
writeVideo(v,F(k))
end
close(v)
But this fails. As above, I'm displaying the k for debugging purposes, and it appears that the cdata sometimes is a 0x0-matrix, which is weird.
The for loop
for k=1:length(F)
disp(size(F(k).cdata))
end
returns

344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0
0 0
0 0
344 436 3
0 0
0 0

Best Answer

Your code does
if size(size(F(k).cdata)) ~= [1, 3]
writeVideo(v,F(k))
end
I recommend you use ndim(F(k).cdata) instead of testing size(size())
Currently you ask to write only if the data does not have 3 dimensions. That occurs if the data is empty -- so you are asking to write only the frames that are empty.
You end up with empty frames because you have
if steg_nummer <50 | rem(steg_nummer,5)==0 % för att snabba på simuleringen plottas inte alla BPM-steg
so you only do the capture for the first 49 frames, and every 5th frame after that. The other ones end up empty.
If only want to skip frames in the output, then you will need to use another variable which is the counter of how many frames you have written.