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

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

 Accepted Answer

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 Comments

Greetings OCDER
after the "%Do what you need to here", I looked at what fullfile meant. If I understand there should be two paths 1) ImageFile path where the Matlab code is saved "/Volumes/RaePond/data/matlab/hypercode.m"
2) ImageFiles path where the images are exactly saved
Is this correct?
FullPath = fullfile(ImageFiles ('/Volumes/RaePond/data/matlab/hypercode.m').folder, ImageFiles ('/Volumes/RaePond/LemImages/plants/Hyp_90).name);
IM= imread(FullPath);
Actually, that'll be incorrect. ImageFiles is a structure, so you can't do ImageFiles('text').folder.
Here, I'm using fullfile so that you can access your image file by full path and not by file name only. Also, fullfile takes care of the OS-dependent file separator issue ('/' vs '\') when making the full file path.
The path where your code is should be added to your matlab path so you can summon it from any working directory.
%This allows you to call hypercode.m while in any working directory
addpath('/Volumes/RaePond/data/matlab/')
%This only assembles the full path to your image
FullPath = fullfile(ImageFiles(i).folder, ImageFiles(i).name)
= '/Volumes/RaePond/LemImages/plants/Hyp_90/2_0_0.png'
%If you try this without the fullfile, you'll probably get an error:
IM = imread(ImageFiles(i).name) %ERROR: can't find file. Not in working dir.
%DO NOT do this:
addpath(genpath('/Volumes/RaePond/')) %This adds all folder + subfolders
IM = imread(ImageFiles(i).name) %POTENTIAL BUG!
%Since your file names have the same name, this will get confused.
%Can't get the same file in a different folder.
%DO THIS. Use the full image path:
FullPath = fullfile(ImageFiles(i).folder, ImageFiles(i).name);
IM = imread(FullPath);
I'm not sure what all your varying folder names are (ex: "main_folder/(variable subfolder??)/Hyp_90/image_file.png"), but you do eventually need to modify the 1st line of code to something like:
MainDir = dir('/Volumes/RaePond/LemImages/');
Hope this helps!
Hi OCDER The path to get to all 1472 main folders is: ('/Volumes/RaePond/LemImages/')
all of the main folders, in the LemImages have barcode names (11_tree_2_2_1). Each barcode folder has a subfolder Hyp_90 that has 1 '.png' and 1 '.txt. file
Overall example:/Volumes/RaePond/LemImages/ -->(11_tree_2_2_1)--> Hyp_90--> '0_0_0.png, 1_0_0.png, 2_0_0.png all the way to 244_0_0.png
I have a side question. When the images are read will they read in order 0_0_0.png, 1_0_0.png, 2_0_0.png all the way to 244_0_0.png? Or will it read 0_0_0.png, 1_0_0.png, 10_0_0.png, 11_0_0.png?
I just thought of this last night.
I'll look over your responce- Thank you
The order is whichever order the OS will return. On windows, it is alphabetical. I presume in Mac it is the same.
Stephen Colbeldick's natsortfiles will let you sort the files numerically.
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
I appreciate the guidance from everyone to help me understand. Things are running smoothly!

Sign in to comment.

More Answers (2)

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.
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})

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 21 May 2018

Commented:

on 24 May 2018

Community Treasure Hunt

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

Start Hunting!