Hi guys. I want to make a sequential folder that can save a bunch of images.
The condition is:
If (Folder already Exists)
The images saved into that folder
else
create new folder+number
the images saved into that folder
I don't know how to build a sequential folder I just can make 1 folder only.
So here is my code:
folder = 'C:\Users\FirdausyAngga\Google Drive\All about TA\FIX TA ku\Codes\remove lines\CropImages';
[parentFolder deepestFolder] = fileparts(folder);
newSubFolder = sprintf('%s/DATA-%s', folder, deepestFolder);
files={'image1','image2','image3','image4','image5,'...
'image6','image7','image8','image9','image10',...
'image11','image12','image13','image14','image15',...
'image16','image17','image18','image19','image20',...
'image21','image22','image23','image24','image25',...
'image26','image27','image28','image29','image30'};
for j = 1 : lastObj
figure(hFig1);% Switch to figure 1.
thisBBoxs = measurements2(j).BoundingBox
[subImage] = imcrop(im_bw, thisBBoxs);
% Create an image filename, and read it in to a variable called imageData.
pngFileName = strcat('image', num2str(j), '.png');
if ~exist(newSubFolder, 'dir')
mkdir(newSubFolder);
write_dir = strcat(newSubFolder, '/', files{j},'.png');
imwrite(subImage, write_dir);
fprintf('File %s saved.\n', pngFileName);
else
mkdir(newSubFolder);
write_dir = strcat(newSubFolder, '/', files{j},'.png');
imwrite(subImage, write_dir);
fprintf('File %s saved.\n', pngFileName);
end
end

2 Kommentare

Stephen23
Stephen23 am 10 Jun. 2017
Bearbeitet: Stephen23 am 11 Jun. 2017
@Firdausy angga Permana: your code is confusing. For example, you hard-code a list of filenames and you also generate exactly the same filenames within the loop. You use the hard-coded names for imwrite, and the identical generated names for fprintf. Why make it so complicated?
It is not really clear what you are trying to achieve, but it appears to be that you want to create a new folder once, and use the same new folder for saving all of the files. Is this correct? The name of this folder seems to be independent of anything that happens inside the loop, so why not simply create it before the loop?
Firdausy angga Permana
Firdausy angga Permana am 11 Jun. 2017
Bearbeitet: Firdausy angga Permana am 11 Jun. 2017
yep i think so this code is confusing me too lol. I want to make new folder when the current folder/files is existed.
When i press run for the first time, the code make new folder and saving a bunch of images. For the second run, the code check there's a folder contain the images or not, if yes create new folder and save images into that new folder.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 10 Jun. 2017
Bearbeitet: Stephen23 am 10 Jun. 2017

0 Stimmen

Making a few guesses here, but it appears that you are trying to do something like this (obviously untested):
folder = 'C:\Users\FirdausyAngga\Google Drive\All about TA\FIX TA ku\Codes\remove lines\CropImages';
%
[parentFolder,deepestFolder] = fileparts(folder);
newSubFolder = fullfile(parentFolder,sprintf('DATA-%s', deepestFolder));
if exist(newSubFolder, 'dir')~=7
mkdir(newSubFolder);
end
%
for kk = 1:lastObj
figure(hFig1);% Switch to figure 1.
thisBBoxs = measurements2(kk).BoundingBox;
subImage = imcrop(im_bw, thisBBoxs);
%
pngFileName = sprintf('image%d.png',kk);
pngFilePath = fullfile(newSubFolder,pngFileName);
%
imwrite(subImage, pngFilePath);
fprintf('File %s saved.\n', pngFileName);
end
Note:
  • I only used fullfile to generate file path strings.
  • I made the new folder once, before the loop.
  • I did not waste time with a hard-coded list of files.

1 Kommentar

Firdausy angga Permana
Firdausy angga Permana am 11 Jun. 2017
thanks for simplify the code really grateful for that it run smoothly!
But, what I mean is when i run for the second time the code check the files and if it exists make new folder and save the files (images) into that(new) folder. Hope you can help me for that. Thanks!

Melden Sie sich an, um zu kommentieren.

Kategorien

Bearbeitet:

am 11 Jun. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by