MATLAB: Do I receive an error when using the ADDFRAME function in MATLAB 6.5 (R13)

3daddframeaviaxesgetframeMATLABrectanglerotate

I am making an AVI file of a rotating image. When I use the following code to rotate my axes and capture frames:
% create an AVI object
aviobj = avifile('myAVI.avi');
% create peaks plot
surf(peaks(30))
% freezes aspect ratio properties to enable rotation
% and turn off all axis lines, tick marks, and labels
axis vis3d off;
for i = 1:30
% rotate view
view(-90-i,18);
% get frame
F = getframe;
% add frame F to aviobj
aviobj = addframe(aviobj,F);
end
% close aviobj
aviobj = close(aviobj);
I receive the following error:
??? Error using ==> avifile/addframe (ValidateFrame)
Frame must be 224 by 280.

Best Answer

We have verified that there is a bug in MATLAB 6.5 (R13) when using GETFRAME with a 3-D axes.
As the axes rotate, GETFRAME captures frames that are different sizes. ADDFRAME expects frames that have the same dimensions as the previous frames.
As a workaround, you can specify the area of the figure you want to capture as an input argument to GETFRAME as illustrated below:
% create an AVI object
aviobj = avifile('myAVI.avi');
% create peaks plot
surf(peaks(30))
% freeze aspect ratio properties to enable rotation
% and turn off all axis lines, tick marks, and labels
axis vis3d off;
% set figure units to pixels
set(gcf,'Units','pixels')
% get position of figure window
rect = get(gcf,'Position');
for i = 1:30
% rotate view
view(-90-i,18);
% get frame of specified size
F = getframe(gcf,[0 0 rect(3) rect(4)]);
% add frame F to aviobj
aviobj = addframe(aviobj,F);
end
% close aviobj
aviobj = close(aviobj);
This example uses the GET function to obtain the "Position" property of the figure. The last two components of "rect" are the width and height of the figure window (excluding the toolbar area). In the call to GETFRAME the second input argument is a position vector which specifies the area to capture. This example will capture the area defined by the rectangle which starts at the lower left-hand corner of the figure window and whose dimensions are the width and height of the figure window. This forces all frames captured by GETFRAME to be the same size.