Load .mat files from subfolders with different names

21 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Jan
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
Chris Shortgeorge
Chris Shortgeorge il 10 Giu 2018
Hello Jan and thanks for your response
First of all, since i' ve got the subfolders' names (First part), how can i have access to the data i want? For example using
for i =1:size(SubFolder,2)
B=SubFolder{i};
load('C:\Temp\...\B\data1.mat');
end
I get the following error Unable to read file 'C:\Temp\...\B\data1.mat'. No such file or directory
Also i ve got Matlab R2015a so is there a way to adjust the second part of the answer?
ignacio bobadilla tapia
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.

Accedi per commentare.

Più risposte (0)

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!

Translated by