How to make a sequential folder?
이전 댓글 표시
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
@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
2017년 6월 11일
편집: Firdausy angga Permana
2017년 6월 11일
답변 (1개)
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.
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!