Minimizing movie creation time in MATLAB

110 Views Asked by At

I am using MATLAB's movie() function to make a movie of a large amount of time series data. With its current instantiation, it will take about 14 hours to finish. What can I do to better optimize it?

The biggest thing I am trying to do is suppress drawing the plot to the screen each time while still updating it for the getframe() function.

An abbreviated version of my code is:

t = 0:1000000; % I have about 10^6 data points
x = sin(t); % Let's pretend the data I'm plotting is the sine function
y = cos(t); % I have multiple data series being plotted in 'snapshots'
num_frames = length(t);
movie_length = 100; % seconds
fps = 60;
for k = 1:num_frames
clf; hold on;
plot(1, x(k),'bo')
plot(2, y(k),'bo')

ax = gca;
ax.XLim = [0 1];
ax.YLim = [-1,1];

f(k) = getframe;
end

movie(f,1,fps)
1

There are 1 best solutions below

0
Patrick Happel On

Here is one version which speeds it up by approximatly a factor of two on my machine. I have reduced the number of points to 10^3.

clear f g; 
t = 0:10^3; % I have about 10^6 data points
x = sin(t); % Let's pretend the data I'm plotting is the sine function
y = cos(t); % I have multiple data series being plotted in 'snapshots'
num_frames = length(t);



tic;
for k = 1:num_frames
    clf; hold on;
    plot(1, x(k),'bo')
    plot(2, y(k),'bo')

    ax = gca;
    ax.XLim = [.8  2.2];
    ax.YLim = [-1,1];

    f(k) = getframe();

end
toc

% This is faster

h_fig = figure;

g(num_frames) = struct('cdata', [], 'colormap', []);
tic;
ax = axes(h_fig);
ax.XLim = [.8 2.2];
ax.YLim = [-1,1];
hold on;
p1 = plot(1,x(1), 'bo');
p2 = plot(2,y(1), 'bo');
drawnow;
g(1) = getframe(ax);
for k = 2:num_frames
    p1.YData = x(k);
    p2.YData = y(k);
    g(k) = getframe(ax);
end

toc

Note that without getframe, the second version is about 100 times faster. thus, If you know how to compute the cdata of a single frame, it might be much faster than to plot the data and use getframe.

One remark: I was not able to copy&paste&run your code without errors. Even if written quickly from mind, it would be nice if you could test it for errors before posting.