Save figure as image to folder with continuous numbering

3 views (last 30 days)
if i use this code all the picture that were there previously are overwritten by the last one. i would like that it names the files like this 1.jpg,2.jpg, and so on . . . and not overwrite the files!
%
filename = fullfile(folder, baseFileName);
img = getframe(gcf);
folder = 'C:\Users\Taiko\Desktop\FalseColor\';
ImageFiles = dir(folder);
a = length(ImageFiles)-1;
for Index = 1:a
baseFileName = [num2str(Index),'.jpg'];
imwrite(img.cdata,fullfile(folder,baseFileName));
end
if exist(filename, 'file')
promptMessage = sprintf('%s already exists.\nDo you want to overwrite it', filename);
titleBarCaption = 'Overwrite?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(buttonText, 'Yes')
imwrite(img.cdata,filename);
end
else
imwrite(img.cdata,filename);
end
edit: i have a script that makes false color images and i would like to whenever i run the script that it saves the output image in that folder and labeling them continuously 1.jpg and so on
  3 Comments
franz binder
franz binder on 6 Aug 2017
Edited: franz binder on 6 Aug 2017
i am new to matlab i was looking for question with similar problem and this one seemd to do what i was looking for excpt it doesn't. i just copied the code from the other question.
for me it is alright if you have a differnt way of doing it. i tried it with and without the test. both didn't not do what i wanted.
basically i have a script that make a false color image (one at a time) and i want that it saves the images to that folder and labeling them 1.png and so on whenever i run the script.(even better would be if it could be labled with the date it was made)
franz binder
franz binder on 7 Aug 2017
i now have this code:
%
img = getframe(gcf);
folder = 'C:\Users\Taiko\Desktop\FalseColor\';
ImageFiles = dir(folder);
a = length(ImageFiles)-1;
for Index = 1:a
baseFileName = [num2str(Index),'.jpg'];
filename=[folder,baseFileName];
kk=Index;
while exist(filename,'file') % keep trying new filenames, each increased by 1 integer, until you find one that doesn't already exist
kk=kk+1;
filename=[folder,num2str(kk),'.jpg'];
end
imwrite(img.cdata,filename);
end
but i now get: 1.jpg(first image) 2.jpg(second image) 3.jpg(second image) 4.jpg(third image) 5.jpg(third image) 6.jpg(third image) 7.jpg(third image)
for the next one i get 8 copys

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Aug 2017
All of this gets easier if you can set up conditions first and then loop doing the getframe and writing files sequentially, instead of each time having to independently figure out which file to write.
But to handle your situation as-is, and presuming that there are no other jpg files in the directory:
img = getframe(gcf);
nowstr = datestr(now, 'yyyymmddHHMM');
folder = 'C:\Users\Taiko\Desktop\FalseColor\';
ImageFiles = dir( fullfile(folder, '*.jpg') );
if isempty(ImageFiles)
next_idx = 1;
else
lastfile = ImageFiles(end).name;
[~, basename, ~] = fileparts(lastfile);
file_number_str = regexp('(?<=.*_)\d+$', basename, 'match' );
if isempty(file_number_str)
error('There is a .jpg file present that does not follow the expected naming pattern: "%s", lastfile);
end
last_idx = str2double(file_number_str);
next_idx = last_idx + 1;
end
newfilename = fullfile( folder, sprintf('%s_%04d.jpg', nowstr, next_idx) );
imwrite(img.cdata, newfilename);
Note: if you expect to write more than 9999 frames in one series, then change the %04d to %05d or larger. Make sure that you keep the 0 after the % though. Not %4d for example.
Most this code could be eliminated if it was permitted to initialize some variables first and then write files in a loop instead of having to figure out what the next file is independently each time.
  2 Comments
Oliver Horrobin
Oliver Horrobin on 1 Feb 2021
Hi there,
I've tried implamenting this but it keeps getting stuck on
error('There is a .jpg file present that does not follow the expected naming pattern: "%s", lastfile);
What does '(?<=.*_)\d+$' do?
Cheers,
Ollie

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!