Creating Video Files with MATLAB

We can create video files, which we then integrate into multimedia publications. In principle, any video material can be used, such as recorded laboratory and field experiments, screen video captures or recorded computer animations. Here I demonstrate how to create a video of an animation with MATLAB.

The sister book MATLAB Recipes for Earth Sciences (Trauth 2015) includes recorded screen activities to demonstrate the use of MATLAB. This material was created with the software Any Screen Record Pro, freely available through the Apple App Store for macOS. A popular free software solution for both macOS and Windows is the Icecream Screen Recorder.

The book MATLAB Recipes for Earth Sciences also contains numerous animated graphics, included as videos. As an example, Chapter 6.4 of the book demonstrates the convolution of a single one within a series of zeros (a unit impulse series, marked with blue circles) with a running mean of length five, using the function conv. The convolution is performed by calculating a series of means (orange circles) for subsets of five data points from the original data series (orange rectangles). To create this video we first create the input data series by typing

clear
t = 1 : 50;
x = [zeros(24,1);ones(1,1);zeros(25,1)];
y = NaN(size(x));

Then we define the size of the window, i.e. the length of the running mean.

w = 5;

Next we prepare the video file amovie_1.avi using VideoWriter, define the frame rate of 1 frame per second and video quality of 100%. We then check the parameter settings and open the file.

v = VideoWriter('amovie_1.avi');
v.FrameRate = 1;
v.Quality = 100;
open(v);

Then we run the animation and record the video by typing

for i = (w+1)/2 : length(x)-(w-1)/2
close all
figure('Color',[1 1 1],...
    'Position',[300 300 1600 800])
y(i) = mean(x(i - (w-1)/2 : i + (w-1)/2));
y(length(x)-(w-1)/2+1:length(x)) = NaN;
p3 = patch([i-(w-1)/2 i+(w-1)/2 ...
    i+(w-1)/2 i-(w-1)/2],...
    [-2 -2 2 2],'r',...
    'FaceColor',[0.8510 0.3255 0.0980],...
    'FaceAlpha',0.3); hold on
s1 = stem(t,x);
s2 = stem(t,y);
s3 = stem(t(i - (w-1)/2 : i + (w-1)/2),...
x(i - (w-1)/2 : i + (w-1)/2),'fill');
s4 = stem(t(i),y(i),'fill');
set(gca,'FontSize',20)
set(s1,'MarkerSize',12)
set(s2,'MarkerSize',12)
set(s3,'MarkerSize',12,...
    'Color',[0 0.4471 0.7412]);
set(s4,'MarkerSize',12,...
    'Color',[0.8510 0.3255 0.0980]);
drawnow
M = getframe(gcf);
writeVideo(v,M);
hold off
end

Finally we close the video file by typing

close(v)

The function writeVideo exports the animation in the Audio Video Interleave (AVI) format, introduced by Microsoft in 1992. We can view, edit and convert the video by using the QuickTime Player, for computers running macOS, the VLC Media Player, available for macOS, Windows and Linux, or any other video player. More advanced video editors are available both for free download (e.g.,  Lightworks for macOS, Windows and Linux, iMovie for macOS) or for purchase (e.g., Adobe Premiere Pro CC for macOS and Windows).