Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
saveas command gets slower at each iteration when saving multiple figures

Subject: saveas command gets slower at each iteration when saving multiple figures

From: Jackson

Date: 5 Nov, 2010 18:28:04

Message: 1 of 4

I have written an mfile which repeatedly calls a function to process some data. That function has an on/off switch for plotting, so I can tell the function to plot figures from the data it is processing if I want.

I have found that when my function calls saveas to save a figure, the time taken to save the figure increases each time the function is called from within the m file Each time saveas is called, it is inside of a function, so at the end of each function I would assume that memory is cleared before the function is executed again. If I run my function in a for loop multiple times, the first iteration takes 1 second to save an image, by iteration 10 it takes nearly 1.5 seconds, and by iteration 20 it takes nearly 2 seconds (using tic toc). I need to go through 50 files and have my code save figures for each of them. I am using saveas in the following way:

    saveas(positiveFFTfig, filepath);

Where my file path has .jpg at the end of it so it knows to save as a jpeg.

Can anybody tell me why each subsequent figure takes longer to save than the previous one, until my code becomes unbearably slow?

Subject: saveas command gets slower at each iteration when saving multiple figures

From: ImageAnalyst

Date: 5 Nov, 2010 19:15:19

Message: 2 of 4

Are you sure hold was off when you displayed new things in the plot?
If hold was on, then you might just be piling stuff on top of each
other (covering up prior things if you're loading images or figures)
and so there is more and more to save each time. Maybe try a "cla"
before you call plot() or image() or whatever.

Subject: saveas command gets slower at each iteration when saving multiple figures

From: Jackson

Date: 5 Nov, 2010 19:35:04

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <06f81c28-8310-4d7e-82f4-a1de5ea6e463@35g2000prt.googlegroups.com>...
> Are you sure hold was off when you displayed new things in the plot?
> If hold was on, then you might just be piling stuff on top of each
> other (covering up prior things if you're loading images or figures)
> and so there is more and more to save each time. Maybe try a "cla"
> before you call plot() or image() or whatever.

I solved my problem but it's a bit strange and I don't completely understand why my solution works. And I also tried hold off but it didn't make a difference.

It was actually the plot() command that was taking a long time not the save command (I adjusted my tic toc benchmarking to determine that). So then I added a close all after each plot was finished and it now runs at the same speed each iteration. I guess I had assumed that since the plots were being done inside of a function, the figures didn't need to be closed each time.

I was plotting with 'Visible' set to off so none of the figures actually displayed on my screen, they were just saved to disk. Apparently closing the current figure and creating a new one each time the function was run solved the problem.

Can anybody explain why this is?

Subject: saveas command gets slower at each iteration when saving multiple figures

From: Jan Simon

Date: 5 Nov, 2010 20:38:04

Message: 4 of 4

Dear Jackson,

> I guess I had assumed that since the plots were being done inside of a function, the figures didn't need to be closed each time.

A wrong assumption.
It seems like your figure is filled more and more by invisible objects.
Adding a new object to a figure takes more time, if the number of already existing objects is large. The old "Schlemihl the painter" problem, because Matlab cannot pre-allocate the needed memory.

The solution is simple: Creating a new figure for each plot is not efficient. Better delete the axes after saving, e.g. with CLF or with DELETE(GCA).

Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us