trying to speed up image exports
Ältere Kommentare anzeigen
Have been spending time running profiler to optimize code. At this point I've got things mostly trimmed down but there are a LOT of graphs, and I can spend an hour in print/saveas exporting images. I was looking at this post
and noticed that export was around 5-10% faster than using saveas. I've tried saving as svg instead of png, sometimes it is faster, sometimes its slower (for the same plots/dataset). There are some old posts from 2010-14 on speed-ups, but I've got to imagine those aren't as fast now that Matlab has native functions for this.
Is there any guidance on how to get improvements on these processes? I can be flexible on file format/resolution etc. for anything that going to be at least 10% faster.
2 Kommentare
Walter Roberson
am 2 Nov. 2024
To clarify:
You have individual axes that you wish to save as images? Or are they figures that you wish to save as images (including the possibility of subplot() or tiledlayout() ?)
Justin Bell
am 2 Nov. 2024
Akzeptierte Antwort
Weitere Antworten (1)
Subhajyoti
am 2 Nov. 2024
Bearbeitet: Subhajyoti
am 2 Nov. 2024
You can export the figures to FIG-files ('.fig') using the “savefig” function. This gives better performance at the major performance bottleneck, which is exporting the figure to external files.
Here, in the following implementation, I have clocked the running time for the function.
tic
% generate random data. Plot them and save the plot to PNG file
for i = 1:1000
%Preparing data for plotting
x = 1:1000;
% generate 1000 random data following normal distribution
y = 255*randn(1,1000);
% Save the plot to PNG file without displaying it
fig = figure('Visible','off');
plot(x,y);
title('Random data','Interpreter','latex');
xlim([0 1000]);
ylim([100 255]);
xlabel('X','Interpreter','latex');
ylabel('Y','Interpreter','latex');
set(gca,'TickLabelInterpreter','latex');
% Save the plot to PNG file
filename = ['plot' num2str(i) '.fig'];
savefig(fig,filename);
close(fig);
end
toc
The above implementation exports at '500 figures/minutes', which is around 5x faster than PNG-format. You can further improve the output time by setting the ‘Visible’ parameter of the ‘figure’ object to ‘off’.
You can open the saved figure using ‘openfig’ from any MATLAB script or Command-Line as follows:
openfig("plot1.fig", "visible")
Refer to the following resources to know more importing and exporting Figures in MATLAB:
- https://www.mathworks.com/help/matlab/ref/savefig.html
- https://www.mathworks.com/help/matlab/ref/openfig.html
Additionally, you can refer to the following resources to know more about saving Plot as Image or Vector Graphics File in MATLAB:
Kategorien
Mehr zu Printing and Saving finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


