Exporting Video from Matlab

It’s rather useful to be able to export video from Matlab of say an animated plot for later sharing. I found this SO thread rather helpful.

I had the best luck with the VideoWriter method, generic code is as follows:

%# figure
figure,set(gcf,'Color','white')
Z = peaks; surf(Z);  axis tight
set(gca,'nextplot','replacechildren','Visible','off');%# preallocate
nFrames =20;
mov(1:nFrames)=struct('cdata',[],'colormap',[]);%# create movie
for k=1:nFrames
   surf(sin(2*pi*k/20)*Z, Z)
   mov(k)= getframe(gca);end
close(gcf)%# save as AVI file,and open it using system video player
movie2avi(mov,'myPeaks1.avi','compression','None','fps',10);
winopen('myPeaks1.avi')

Depending on what you actually want to capture, you may need to choose a different handle, in my case I used the handle of the figure itself so I could also get the axes and the text I used for output displays.

Using this technique, andĀ this code, I was able to make a video of my prototype 1D DG code.

Leave a Reply

Your email address will not be published. Required fields are marked *