I am learning to make a movie in Matlab. I coped this piece of code from internet to make the movie. I am using OS X Mavericks and Matlab 2013a. I usually dock the figure windows. When I run the code I can see animation perfectly. When when I save the movie either by using movie2avi or VideoWriter command. It doesn't record the movie with full frame of figure. Instead I see a little bit of the figure window and my Matlab desktop view. Can anyone help me with this?
clear;
close all;
% Creates a 2D Mesh to plot surface
x=linspace(0,1,100);
[X,Y] = meshgrid(x,x);
N=100; % Number of frames
for i = 1:N
% Example of plot
Z = sin(2*pi*(X-i/N)).*sin(2*pi*(Y-i/N));
surf(X,Y,Z)
% Store the frame
M(i)=getframe(gca); % leaving gcf out crops the frame in the movie.
end
% myVideo = VideoWriter('wavemotion.avi');
% open(myVideo);
% writeVideo(myVideo,M)
% close(myVideo)
% Output the movie as an avi file
%movie2avi(M,'WaveMovie.avi');
Based on the documentation of both
VideoWriterandmovie2avi, you need to add a few more instructions to your code.First, you need to set the renderer for a nice display and set the axes that the next plot will reset only the children and not the entire axes:
There is also an advice to preallocate the frame buffer:
Finally, you just need to call
getframewithout any argmuent, asgcais the default argument.On my computer (with Matlab 2013b), with these modifications and using
VideoWriter, it works fine. Withmovie2avi, it does not work properly; I think I need to specify a compression codec to fix that.