How to loop through all files present in several subfolders in a main folder?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Iugo
el 24 de Feb. de 2021
Comentada: Iugo
el 24 de Feb. de 2021
Hello!!
I have a main folder (with this directory D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal) and 16 subfolders (Caltech, KKI, Leuven, Stanford,etc..) in it, with each subfolder containing several *mat files within them. I want to create a loop that goes through all the files coming from all the subfolders and use each file for then perform some calculations.
Thanks in advance!
0 comentarios
Respuesta aceptada
Walter Roberson
el 24 de Feb. de 2021
Editada: Walter Roberson
el 24 de Feb. de 2021
You can use a filedatastore with a reader function that loads the appropriate variables from the .mat files.
Or you can use dir with ** wildcard:
dinfo = dir('D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\**\*.mat');
filenames = fullfile({dinfo.folder}, {dinfo.name});
numfiles = length(filenames);
for K = 1 : numfiles
thisfile = filenames{K};
do something with thisfile
end
3 comentarios
Walter Roberson
el 24 de Feb. de 2021
It already does that in practice. Not really guaranteed, though. You could guarantee it by sort(filenames) before you loop.
Más respuestas (0)
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!