Line 129 in saveas function is slow!

14 views (last 30 days)
Justin
Justin on 4 Aug 2011
I'm plotting some figures in a script where I create a single figure and then clear the figure and replot to the current axes. I save the figures using saveas after each figure is plotted. Saveas is called 16 times (8 times it saves as a .fig and 8 it saves as a .ai). My function is taking nearly 5 minutes to run with 98.9% of that being saveas line 129 which is this:
if ~isempty(format) && any( exist( ['saveas' format]) == [2 3 5 6] ) %#ok
It doesn't make sense to me that this line should take so long. I'm using Matlab 2007b. Here is the profiler output.
Lines where the most time was spent Line NumberCodeCallsTotal Time% TimeTime Plot 129if ~isempty(format) &&...16 280.099 s98.9% 130feval( ['saveas' format], h, n...81.907 s 0.7% 159print( h, name, ['-d' dev{i}] ...81.121 s 0.4% 136[ops,dev,ext] = printtables; %...80.017 s0.0% 160return80 s0%All other lines 0 s0% Totals 283.145 s100%
Thanks in advance.
Justin

Answers (2)

Oliver Woodford
Oliver Woodford on 5 Aug 2011
If the question were instead "How can I make saveas faster?", I'd say -
Change line 129 to:
if ~isempty(format) && ~isempty(strmatch(format, char('fig', 'm', 'mfig', 'mmat'), 'exact'))
  1 Comment
Fangjun Jiang
Fangjun Jiang on 5 Aug 2011
Yes, good idea! The rest of saveas.m did use strmatch( , ,'exact') to compare other format such as .bmp or .eps. This could be a nice enhancement of the saveas() function.

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 4 Aug 2011
It is true for me too. That line consumes more than half of the time if I save the plot to a .fig file and repeat it a dozen times.
You could put it in a good way that the other part of the code is fast.
To understand it further, the time consuming part is the exist() function, it tries to see if a file named 'saveasfig' exists in the MATLAB path. It does and it's a private function. It could also search for saveasfig.mex, saveasfig.p. That is what [2 3 5 6] for.
It sounds unreasonable that it spent 280 seconds on that line of code, unless your computer is really old and slow. I suggest you check your MATLAB path and remove unnecessary folders in it. Run rehash might also help.
>> which saveasfig -all
.\MATLAB\R2007b\toolbox\matlab\general\private\saveasfig.m % Private to general
  1 Comment
Justin
Justin on 4 Aug 2011
My computer isn't old, it's practically new. It's a 64 bit machine running 32 bit Matlab with Windows 7.
I'll try rehash. It could also be that I have a lot of folders in my MATLAB path. I'll also try limiting those. Thanks!

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!