Create a filelist with files (ending in _BTcom.mat) from different subfolders (all the subfolders are in the same folder)
Show older comments
HI,
I am trying create a filelist from files in different subfolders. All the files are in the same main folder 'C:\Users\paola\Desktop\MATLAB\20mM-new'. I wrote this but it doesn't really work
folder='C:\Users\paola\Desktop\MATLAB\20mM-new'
d=dir(folder)
subf=d([d.isdir])
for k=1:numel(subf)
subfolder=subf(k).name
subf1=fullfile(folder,subfolder)
f=dir([subf1 '\*_BTcom.mat'])
for ii=1:numel(f)
file=fullfile(subf1,f(ii).name);
end
end
Answers (1)
Thomas Jensen
on 13 Apr 2021
0 votes
Hi Paola,
The best approach to search for files inside subfolders is creating a recursive function.
Tou can check https://nl.mathworks.com/matlabcentral/answers/92761-how-can-i-recursively-process-files-in-subdirectories-using-matlab for more information.
4 Comments
Thomas Jensen
on 13 Apr 2021
Hi Paola,
This script will do what you described.
function my_filelist(folder)
fileList = dir(folder);
for i = 1 : length(fileList)
if ~ismember(fileList(i).name,{'.','..'})
if fileList(i).isdir
my_filelist(fullfile(folder,fileList(i).name));
elseif endsWith(fileList(i).name,'_BTcom.mat')
disp(fullfile(folder,fileList(i).name));
end
end
end
end
Paola
on 13 Apr 2021
Thomas Jensen
on 13 Apr 2021
Hi Paola,
Sorry it did not work for you. I tested on Matlab 2017b and it worked fine. The file list is displayed in the command window.
Could you please confirm that you are calling the function my_filelist('C:\Users\paola\Desktop\MATLAB\20mM-new') and what is displayed in the command window?
Best regards,
Paola
on 13 Apr 2021
Categories
Find more on MATLAB Compiler 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!