figure(1); clf; set(1,'color','w'); hold on; axis([-1 1 -1 1]); axis image; axis off; numframes = 20; x = linspace(-1,1,20); y = linspace(-1,1,20); [x,y]=meshgrid(x,y); t = linspace(0,4*pi,numframes); M = moviein(numframes); for n=1:numframes, cla; z = sin(4*x-sin(t(n))).*cos(4*y-cos(t(n))); surf(x,y,z); shading interp; M(:,n) = getframe; pause(0); end movie(M);
>> mpgwrite(M,colormap,'movie_file.mpg');
If this does not work then you are using a version of Matlab that is too new for mpgwrite to work. Mpgwrite assumes the matrix M is 2-d, while newer versions of Matlab (5.3 or greater) have M as a struct array. You will have to write your m-file accordingly to account for this change.
>> !xanim movie_file.mpg
Don't worry if the movie doesn't come out very well with xanim. Converting it to a gif will improve it.
>> !convert -crop 0x0 -geometry 25% -loopback 0 movie_file.mpg movie_file.gif
with the following options supported by convert: