问题描述
我正在将大量图形打印为.png文件.每个图都是数据矩阵中一列的图,然后我将.png文件放入一个动画中.
I am printing a large series of figures as .png files. Each figure is a plot of a column from a data matrix, and I take the .png files and string them together into an animation.
我的问题是前几百个图像打印速度很快,但是创建每个新图形的时间却迅速增加,从前几百个.png文件的约0.2秒到第800个图形左右的2秒甚至更多
My problem is that the first few hundred images print quickly, but the amount of time to create each new figure increases rapidly, from ~0.2 sec for the first few hundred .png files to 2 sec or more at around the 800th figure.
在脚本运行期间,内存使用量增加,但每隔几秒钟仅增加1MB.这是在运行R2009b 64位的Windows上.
Memory usage increases during the run of the script, but only by 1MB every few seconds or so. This is on Windows running R2009b 64-bit.
我的代码如下:
n = 1000;
matrix = rand(n);
f = figure('Visible','off'); % create the figure
for i_ =1:n
plot(1:n,matrix(:,i_));
ylim([0 1]);
set(f,'PaperUnits','inches','PaperPosition',[0 0 6 4]);
png_name = [ 'img/timestep_' sprintf('%05d',i_) ];
print('-dpng', png_name);
end
推荐答案
尝试不重新生成图,而仅在每次迭代时更改XData
和YData
属性:
Try not to regenerate the plot, but only change the XData
and YData
properties at each iteration:
set(f,'PaperUnits','inches','PaperPosition',[0 0 6 4]);
h = plot(1, matrix(:,1));
ylim([0 1]);
for i_ = 1:n
set(h, 'XData', 1:n, 'YData', matrix(:,i_))
png_name = sprintf('img/timestep_%05d',i_);
print('-dpng', png_name);
end
另一个建议.如果要创建动画,为什么要生成png文件?使用 GETFRAME 并创建电影直接在MATLAB中.
Another suggestion. If you want to create an animation, why are you generating png files? Use GETFRAME and make a MOVIE directly in MATLAB.
这篇关于为什么在打印许多(.png)图形时MATLAB会变慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!