How to go through multiple different naming folders for the same subfolder name

10 vues (au cours des 30 derniers jours)
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

Réponse acceptée

OCDER
OCDER le 21 Mai 2018
Modifié(e) : OCDER le 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 commentaires
OCDER
OCDER le 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
Rae Pond
Rae Pond le 24 Mai 2018
I appreciate the guidance from everyone to help me understand. Things are running smoothly!

Connectez-vous pour commenter.

Plus de réponses (2)

Ameer Hamza
Ameer Hamza le 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 commentaire
Guillaume
Guillaume le 21 Mai 2018
dir has been able to perform recursive listing for a few versions now.

Connectez-vous pour commenter.


Guillaume
Guillaume le 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})

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by