How to assign names to save different figures?

40 views (last 30 days)
Hi all,
I'm cropping out certain parts of an image using imcrop function, which opens them in new figures. I would then like to save those figures as separate .tif images into a specific folder and then repeat for other images. In such a way in one folder I would like to write new images as 'figure_%d.tif' after the already existent images. Here's the code I have at the moment:
files = dir( fullfile(folder_name,'*tif') ); %list all figure files
file = {files.name};
name=file{:,end};
shortname1=name(1:6);%extract 'figure' from the file to make sure it's of the same kind
if shortname1=='figure'
shortname2=name(1:end-4);%find the number of file
n=str2num(shortname2(end));%returns the number of the file
for k=(n+1):(n+numel(figures))
baseFileName = sprintf('figure_%d.tif',k);
fullFileName = fullfile(folder_name,baseFileName);
figlist=findobj(gcf);%find all images in opened figures
for i=1:numel(figlist)
saveas(figlist(i),fullFileName);
end
end
end
The problem is, it saves only the last opened figure several times instead of saving each figure with a set baseFileName. Could anyone please help me to sort it out?
Thanks in advance!

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Jun 2014
I think that the problem is with the inner for loop. The code seems to know already about the number of figures that it has to write to file from the figures list
for k=(n+1):(n+numel(figures))
baseFileName = sprintf('figure_%d.tif',k);
fullFileName = fullfile(folder_name,baseFileName);
% etc.
(Is this array initialized with something like figures = findobj('Type','figure'), and so contains all the handles to the open figures?)
So we have the new file name and the figure, but then the following call is made to get all objects and descendants associated with gcf, the "current figure"
figlist=findobj(gcf);
Then the code iterates on the length of this object, writing each element in the list to the same output file. And this happens repeatedly since for every k we compute figlist=findobj(gcf); against the same current figure.
I think that all you need to do is remove the inner for loop and have something more like
i=1; % to access the ith element of figures
for k=(n+1):(n+numel(figures))
baseFileName = sprintf('figure_%d.tif',k);
fullFileName = fullfile(folder_name,baseFileName);
saveas(figures(i),fullFileName);
i = i+1; % increment
end
The above assumes that figure is an array of all figure handles that you want to save to file.
  1 Comment
Sofya
Sofya on 27 Jun 2014
Yes, that's right, I got lost in my loops. Now it works perfectly. Thank you very much for your help!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 27 Jun 2014
If you want to give a particular name to the figure's title bar, you can do this:
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Or if you want to embed some variable's value into the name, you can do this:
% Give a name to the title bar.
itsName = sprintf('The parameter value in this figure is %.3f', someValue);
set(gcf, 'Name', itsName, 'NumberTitle', 'Off')

Community Treasure Hunt

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

Start Hunting!