figures created in a loop are incrementing in size

I have some code that is creating and saving the figures that I want it to, but I have noticed that the size of each .fig file is gradually incrementing, starting at about 2Mb and increasing after 600 plots to 160Mb (which fairly obviously is a problem in terms of disk space usage!).
I would like to know how to improve my code so that this problem doesn't occur (and to know why it IS occurring). Thanks.
for ii=1:11
for jj = 1:5
for kk=1:6
filename = sprintf('man_%d_%d_%d',ii,jj,kk);
AppFilt2(filename);
end
end
end
function AppFilt2(filename)
load(filename);
[some other stuff that has nothing to do with figures]
%plot right
clear fig;
g=@plot;
g(R,'Color',[0 0 0.6]);
newname = [filename '-unfiltR.fig'];
hgsave(newname);
clear fig;
gg=@plot;
gg(Rf,'b')
newname = [filename '-filteredR.fig'];
hgsave(newname);
%plot left
clear fig;
h=@plot;
h(L,'Color',[0.6 0 0.6]);
newname = [filename '-unfiltL.fig'];
hgsave(newname);
clear fig;
hh=@plot;
hh(Lf,'m');
newname = [filename '-filteredL.fig'];
hgsave(newname);
%plot right over left
hold on
ggg=@plot;
ggg(Rf,'b')
newname = [filename '-filteredboth.fig'];
hgsave(newname);
end

 Accepted Answer

I don't understand what do you mean by "clear fig;". If you mean close the figure, you need to use close(FigureHandle). "clear fig" means clear the variable "fig", which you don't have.
Why do you need to use hh=@plot? Why don't use plot() directly? You can use h=plot() to create the figure handle and then close(h) after saving it.
You have "hold on" at the end of your code. Are you sure whether it holds to your next iteration? You might actually save more and more stuff because of that.

1 Comment

I realised later that I needed a "hold off" (and that "clear fig" is nonsense), and thus fixed it, but this and your other points were all helpful. Thank you for your assistance.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!