How to go through multiple different naming folders for the same subfolder name
조회 수: 7 (최근 30일)
이전 댓글 표시
Greetings I have, on a Mac, 1472 main folders(dates+barcode) and within each of those, I have a subfolder (Hyp_90) that contains 244 images. The second image 2_0_0 needs to be skipped as it is a blank.
The information in all of the subfolders is named the same (even down to the blank image) the only thing that changes is the date and barcode of each main folder.
I have read the Matlab 'dir()' reading material but I wonder if I should also be looking at something else
Any guidance is appreciated and Thank you
댓글 수: 0
채택된 답변
OCDER
2018년 5월 21일
편집: OCDER
2018년 5월 21일
MainDir = dir(cd); %Instead of cd, use the folder storing your main folders
MainDir = MainDir([MainDir.isdir]); %Select only the folders
MainDir = MainDir(arrayfun(@(x) ~endsWith(x.name, {'.', '..'}), MainDir)); %Get rid of the . and .. folders
for j = 1:length(MainDir)
ImageFiles = dir(fullfile(MainDir(j).name, 'Hyp_90', '*.png')); %Look at the Hyp_90 subfolder, and png (or tif?) files
for i = 1:length(ImageFiles)
if strcmpi(ImageFiles(i).name, '2_0_0.png') %Skip this one
continue
end
%DO WHAT YOU NEED TO HERE
FullPath = fullfile(ImageFiles(i).folder, ImageFiles(i).name);
IM = imread(FullPath);
end
end
댓글 수: 6
OCDER
2018년 5월 22일
Putting everything together with Guillaume's suggestions:
MainDir = '/Volumes/RaePond/LemImages/';
ImageDir = dir(fullfile(MainDir, '*', 'Hyp_90', '*.png')); %Wildcard search for png files
ImageFiles = fullfile({ImageDir.folder}, {ImageDir.name})'; %Assemble full file names
ImageFiles(strcmp{ImageDir.name}, '2_0_0.png') = []; %Remove the 2_0_0.png files
ImageFiles = natsortfiles(ImageFiles); %Sort file names by folder and name
%Asuming you're doing folder-by-folder operations, get unique folder index
ImagePaths = cellfun(@fileparts, ImageFiles, 'un', false); %fileparts removes the "*.png" part from the full path.
%cellfun operates on each cell element in ImageFiles.
[~, ~, UnqIdx] = unique(ImagePaths, 'stable'); %you need "stable" to prevent ruining the folder sort order
%For each unique image folder, operate on all images in order
for j = 1:max(UnqIdx)
CurFiles = ImageFiles(UnqIdx == j); %The image files in one folder
for k = 1:length(CurFiles)
IM = imread(CurFiles{k}); %The operation you want to do here
end
end
추가 답변 (2개)
Ameer Hamza
2018년 5월 21일
See this FEX submission: https://www.mathworks.com/matlabcentral/fileexchange/15859-subdir--a-recursive-file-search. Unlike dir which only search current folder. It will recursively search folder and its subfolders.
댓글 수: 1
Guillaume
2018년 5월 22일
dir has been able to perform recursive searches for a few versions now, so if all you want to do is get a list of all the files in all the subdirectories, then:
rootfolder = 'Volumes/SomeFolder/Somewhere/'; %folder with the 1472 subfolders
allfiles = dir(fullfile(rootfolder, '*', 'Hyp_90', '*.png'));
allfiles(strcmp({allfiles.name}, '2_0_0.png')) = []; %get rid of the 2_0_0.png images in the list
If you need to build the full path to all these files, it's simply:
fullfile({allfiles.folder}, {allfiles.name})
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!