1

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');
  • I had the same problem. You can find the solution here http://stackoverflow.com/questions/31161480/matlab-videowriter-creates-video-with-only-section-of-figure/31161824#31161824 – Terranees Jul 01 '15 at 12:58

1 Answers1

0

Based on the documentation of both VideoWriter and movie2avi, 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:

set(gca,'nextplot','replacechildren');
set(gcf,'Renderer','zbuffer');

There is also an advice to preallocate the frame buffer:

M(N) = struct('cdata',[],'colormap',[]);

Finally, you just need to call getframe without any argmuent, as gca is the default argument.

On my computer (with Matlab 2013b), with these modifications and using VideoWriter, it works fine. With movie2avi, it does not work properly; I think I need to specify a compression codec to fix that.

Bentoy13
  • 4,886
  • 1
  • 20
  • 33
  • I have copy pasted your solution but result is still the same. Do you also use Matlab on OS X? – user2047050 Jul 08 '14 at 13:20
  • No, on Windows, sorry. Maybe you need to choose a correct compression codec? I've read that this is quite different along OS... – Bentoy13 Jul 08 '14 at 13:21
  • Any place where I can get some info? – user2047050 Jul 08 '14 at 15:11
  • Take a look at this [other post](http://stackoverflow.com/questions/8583538/problems-with-movie-file-creation-in-matlab), there may be a useful trick. Otherwise, I find that `axis tight` may work wrong; try to adjust the axes limits by hand. – Bentoy13 Jul 09 '14 at 07:21