最大限度地减少 MATLAB 中的影片创建时间



我正在使用 MATLAB 的movie()函数制作大量时间序列数据的电影。使用其当前实例化,大约需要 14 小时才能完成。我该怎么做才能更好地优化它?

我试图做的最重要的事情是禁止每次将绘图绘制到屏幕上,同时仍然为 getframe() 函数更新它。

我的代码的缩写版本是:

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)

这是一个版本,在我的机器上,它的速度大约是两倍。我已将点数减少到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

请注意,如果没有getframe,第二个版本大约快 100 倍。 因此,如果您知道如何计算单个帧的cdata,它可能比绘制数据并使用getframe要快得多。

一句话:我无法复制粘贴和运行您的代码而没有错误。即使从脑海中快速编写,如果您可以在发布之前测试它是否存在错误,那就太好了。

最新更新