When the Camera properties change, the dimensions of the axes in which the 3D plot is rendered change slightly. By default, the "getframe" function captures the current axes and, therefore, not all frames will have the same size. Then, the "movie2avi" function throws an error because a movie file can only be created if all frames have the same size.
The "getframe" function can be used to capture the entire figure, instead of only capturing the axes. Using "getframe" with a figure handle ensures that all frames have the same size. In addition, the title of the plot and eventual other elements in the figure will be also included in the movie.
The following piece of code uses the "getframe" function on the handle to the figure containing the 3D plot. Also, note that it is recommended to use "VideoWriter" instead of "movie2avi." "VideoWriter" is faster and more reliable. Finally, if you are using MATLAB R2014a or earlier, the renderer should be set to Z-Buffer to avoid a bug in OpenGL that affects MATLAB R2014a and earlier versions.
writerObj = VideoWriter('movie.avi');
open(writerObj);
f = figure;
width = 800;
height = 600;
set(f,'Position',[15 15 width height])
set(f,'Renderer','ZBuffer')
sphere
axis('square')
title('Rotation of a sphere...')
xlabel('x')
ylabel('y')
zlabel('z')
for az = -50 : .2 : 30
view(az, 40)
frame = getframe(f);
writeVideo(writerObj,frame);
end
for el = 40 : -.2 : -30
view(30, el)
frame = getframe(f);
writeVideo(writerObj,frame);
end
close(writerObj);
Best Answer