reading files of folders with specific name styles

2 views (last 30 days)
Hello,
I am attempting to look for the codes required to open specific folders withing a particular folder, as shown below.
Within this directory, I wish to read the files of the folders with only Att in their names. So, in this case, only files in folders Att10, Att20, Att30, and Att40 will be read. Each folder contains 100 files which will be then used for image processing.
I am attempting to use two for loops:
as in,
First for loop to open each folder,
the second for loop to process each image in each folder.
Can one please help with this?
Kind regards,
Nilesh

Answers (2)

MarKf
MarKf on 1 Dec 2022
Adapt, test, correct, or simplify following code:
Dir_with_Att = 'C:\Users\etc\put_your_directory_path_here\maybe_use_fullfile_here_too';
attdirs = dir(fullfile(Dir_with_Att,'Att*'));
attdirnames = {attdirs(:).name}; % just the dir names
attpaths = (fullfile(Dir_with_Att,attdirnames)); % full paths
for idr = 1:numel(attdirnames)
apath = attpaths{idr};
afiles = dir(fullfile(apath,'*.png')); % or .jpg etc
for idf = 1:numel(afiles)
path2img = fullfile(apath, afiles(idf).name);
%do stuff to img
end
end

Stephen23
Stephen23 on 1 Dec 2022
P = 'absolute or relative path to where the folders are';
S = dir(fullfile(P,'Att*','*.*')); % specify the file extensions
for k = 1:numel(S)
F = fullfile(S(k.folder,S(k).name));
... do whatever with filename F
end

Categories

Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!