Save figure as image to folder with continuous numbering
3 views (last 30 days)
Show older comments
franz binder
on 6 Aug 2017
Commented: Oliver Horrobin
on 1 Feb 2021
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
i got the code from this post put it got never resolved: https://de.mathworks.com/matlabcentral/answers/289707-how-to-prevent-image-that-has-been-stored-not-overwrite-using-imwrite
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
Accepted Answer
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
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
More Answers (0)
See Also
Categories
Find more on Convert Image Type 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!