random error with frame size making a video

Hi everyone,
I am quite surprised and stuck with this problem.
I am processing some results, so I create a series of figures by using the commands:
contourf (eii, v, 'linestyle','none'); axis equal
xlim([0 401]); ylim([0 201]); colorbar; caxis([min(v) max(v)])
where eii is my variable to be plotted {size(eii)=[201 401])} and v is a matrix with the values I want to be plotted.
Then, I make an avi video with another loop and the commands:
open(nameoffigure);
legend('sometext', 'Location', 'South');
Mv(ff)=getframe;
close
where ff is a counter for the diffent figures I am opening. And, after the loop, the commands:
movie(Mv, 1, 1)
movie2avi(Mv, 'nameoffile', 'fps',3)
Well, I have made hundreds of videos this way. But, just from yesterday, without having change anything, I am receiving randomly some messages saying that "Error using avifile/addframe>ValidateFrame Frame must be 370 by 186."
I am puzzled of why this error appears now even when I try to make again videos with figures that I used for making a video just few days ago.
I have been googleing, but I cannot fix this. It seems to me that matlab resize the frame authomatically and some days ago it did it right but now it is doingly wrong by some reason. So, how can I set the frame size in order to avoid this? Or there is other reason for this problem?

6 comentarios

rob
rob el 11 de Dic. de 2014
Editada: rob el 11 de Dic. de 2014
Solution that worked for me: write "drawnow" before "getframe" command, and throw in a "pause" command. The problem is that the capture is done faster than the figure drawing sometimes...
David Pesetsky
David Pesetsky el 31 de Jul. de 2016
This bug is hitting me now. I never know how many seconds to pause, but that does do the trick. And slows everything down. I wonder if the drawnow alone would take care of it?
Tong Zhao
Tong Zhao el 12 de Sept. de 2021
The 'pause' command before getFrame() worked for me too. Also keep in mind it's best practice to use getFrame(gcf) instead of getFrame(gca), because the gca option somehow will produce variable cdata sizes again..
Walter Roberson
Walter Roberson el 12 de Sept. de 2021
The width and height of an axes can vary slightly depending on the axes limits, especially if the axes labels vary. The positioning code does keep the sizes close to the desired size, but if I recall correctly, the way the final label is placed depends a little on the absolute coordinates of the label, not just the relative coordinates. My memory is that an x axes can end up positioned up to 2 pixels shorter than normal, or up to 3 pixels longer than normal.
Tong Zhao
Tong Zhao el 13 de Sept. de 2021
@Walter Roberson Yes I think you're correct. If I use getFrame(gca), I'll get a sequence of cdata with sizes like: 798x600, 799x599, 799x598, etc.
Walter Roberson
Walter Roberson el 13 de Sept. de 2021
One work-around is to imresize() to the desired size.

Iniciar sesión para comentar.

Respuestas (5)

Minoru Tsuzaki
Minoru Tsuzaki el 9 de Jun. de 2012
Editada: Walter Roberson el 31 de Jul. de 2016
Hi, Juan
I've got the same problem. My suggestion of the workaround is to change the line:
Mv(ff) = getframe;
to:
Mv(ff)=getframe(gcf);
The (current, or new?) default behavior of the function, "getframe" is to getframe(gca) rather than get frame(gcf). My case was that I changes the camera position at every frame, which changes the size of data. I don't know the details of your figures, but I suppose a similar change may happen between frames. I don't either know why it did not happen before, but hope that they modify the environment for a certain higher goal ;-)

4 comentarios

Juan Luis
Juan Luis el 9 de Jun. de 2012
Thanks Minuru. I tried that some time ago but it didn't work for me...
Christopher
Christopher el 21 de Nov. de 2013
Thanks it worked for me! I did also change the view point every step!
Tadashi
Tadashi el 10 de Jul. de 2018
Thanks! This also worked for me.
Mario
Mario el 1 de Jun. de 2020
Thanks! That also worked for me in matlab R2019b.

Iniciar sesión para comentar.

Brandon Dewberry
Brandon Dewberry el 28 de Feb. de 2013

2 votos

I had the same issue but found the answer.
I was attempting to save two different frames (plot windows) to the same movie. One of these plot windows had the figure toolbar showing, while the other did not. This caused the number of pixels in frames(end).cdata to be off by ~20 rows.
Once I turned the toolbar off in the second plot window and made sure size(frames(ii).cdata) was the same for all ii the problem went away.

1 comentario

MoonKnight
MoonKnight el 3 de Oct. de 2013
I had exactly the same issue and your solution works for me. Thank you!

Iniciar sesión para comentar.

Roman Voronov
Roman Voronov el 29 de Mayo de 2013

1 voto

a stupid workaround is to use make a tiff stack instead of a movie. you can use the http://www.mathworks.com/help/matlab/ref/frame2im.html command, resize all the images appropriately and then write them by appending a tiff file. tiffs can be animated in software like ImageJ
Shobhan Roy
Shobhan Roy el 6 de Ag. de 2018
Hi,
'pause' indeed works.
I was bugged by this problem for a long time. The issue with getframe is that it sometimes captures the frame before the figure itself can fully load; hence it ends up capturing a smaller frame-size. I especially noticed that the occurrence of the error was not consistent, meaning on some occasions my hardware was able to load the frame before getframe.
I recommend defining the figure window with all subplots, and then giving a generous pause of 1 second before moving on.
figure;
subplot(2,1,1);
subplot(2,1,2);
pause(1)

2 comentarios

Christoph
Christoph el 4 de Oct. de 2019
Pausing worked for me, too.
Thanks a lot!
The best practice is :
desiredPosition = [left bottom width height]
set(gcf,'Position',desiredPosition)
pause(0.01) % doesn't have to be long
getFrame(gcf)

Iniciar sesión para comentar.

Onomitra Ghosh
Onomitra Ghosh el 16 de Mzo. de 2012
Based on the error message, it seems that the not all of the frames in Mv are of the same size. Something in the way the figure is displayed is changing the size. You can confirm that by adding the following after the GETFRAME command:
size(Mv(ff).cdata)
You can have Figures (and corresponding frame captures) of the same sizes by modifying the Position property of the Figure window

2 comentarios

Juan Luis
Juan Luis el 16 de Mzo. de 2012
Yes, I knew that...
I have different examples with the same error, so I have the opportunity of test different things.
I have checked the properties I know (with inspect(gcf) and inspect(gca). Some of the figures fixed when I modify the Position, some others fixed when I modify the Zlim. But still I got some models in which all the properties in the figure (inspect(gcf)) and in the axes (inspect(gca)) where the same. However, I can see very small differences at the position of the colorbar, which I cannot change with "set" (because I don´t know the command to do it). But, apparently, the workaround was to set "colorbar('off'). I am still testing, but, till the moments, I can make all the videos again just by setting "colorbar('off')" after the "open(nameoffigure)" command...
Therefore, still working on it... And still wondering why this appears, and how to fix it properly.... ¿any ideas?
Onomitra Ghosh
Onomitra Ghosh el 19 de Mzo. de 2012
I have not tried it myself but found another thread that discusses COLORBAR positioning.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/270800
May be this will help you to get an exact figure window every time.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Mzo. de 2012

Comentada:

el 13 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by