reading files of folders with specific name styles

2 次查看(过去 30 天)
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

回答(2 个)

MarKf
MarKf 2022-12-1
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 2022-12-1
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

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by