How to go through multiple different naming folders for the same subfolder name
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
OCDER
am 21 Mai 2018
Bearbeitet: OCDER
am 21 Mai 2018
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 Kommentare
OCDER
am 22 Mai 2018
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
Weitere Antworten (2)
Ameer Hamza
am 21 Mai 2018
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 Kommentar
Guillaume
am 22 Mai 2018
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 Kommentare
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!