Load .mat files from subfolders with different names
21 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello everyone,
I have Folder1 which contains a number of subfolders with different names (subfolder_1,subfolder_th etc). Each subfolder has 7 .mat files with the same name (data1,data2,....data7). So i've got two questions:
1. Is it feasible somehow to open one subfolder at a time in a for loop in order to process tha data?
2. Is there a way to load for example data1 from all subfolders and then do the same thing for the other data?
*i guess the main problem is the subfolder's name detection and storing.
0 Commenti
Risposta accettata
Jan
il 9 Giu 2018
Modificato: Voss
il 16 Nov 2023
You can get the list of names of the subfolders easily:
Base = 'C:\Temp'; % adjust to your needs
List = dir(fullfile(Base, '*.*'));
List = List([List.isdir]);
SubFolder = {List.name};
SubFolder(ismember(SubFolder, {'.', '..'})) = [];
You can obtain a list of absolute paths to a specific file name also:
Base = 'C:\Temp'; % adjust to your needs
List = dir(fullfile(Base, '**', 'data1.mat')); % >= Matlab R2016b !!!
Files = fullfile(List.folder, List.name);
Now you can load the data in a loop:
for iFile = 1:numel(Files)
Data = load(Files{iFile})
... process the data
end
2 Commenti
ignacio bobadilla tapia
il 20 Mag 2021
@Jan Dear I have the same problem, how can I store the files by means of a for loop, since as the subfolders change, the files from the first folder continue to appear, they cannot change. Thanks greetings.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Search Path in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!