Azzera filtri
Azzera filtri

Save data from subfolders into a cell array

2 visualizzazioni (ultimi 30 giorni)
Hi all,
I am trying to access several matrices from my directiry and store them in a cell array.
They are stored in a way where I have 1062 subfolders in my directory that are named with the subject ID. Each of that subfolder contains 4 individual .csv files that are all named the exact same across all subjects.
What I would like to do now is go into each of the subfolder, extract one of the 4 csv.files and save it as a matrix in a cell array. Optimally, the information to which subfolder (aka subject) the matrix belongs would also be included in the cell array.
Any help would be appreciated!
Thanks

Risposta accettata

Stephen23
Stephen23 il 4 Feb 2022
Modificato: Stephen23 il 4 Feb 2022
P = 'absolute or relative path to where the subfolders are';
N = 'the name of the file that you want.CSV';
S = dir(fullfile(P,'*'));
S = S([S.isdir]); % remove files
S(ismember({S.name},{'.','..'})) = []; % remove dot directories
for k = 1:numel(S) % loop over the subfolders
F = fullfile(P,S(k).name,N);
S(k).data = readmatrix(F);
end
The imported data are simply stored in the structure S. For example the second file:
S(2).name % subject ID
S(2).data % imported file data
You can trivially loop over the structure and use indexing to process all of the file data.
  8 Commenti
Stephen23
Stephen23 il 4 Feb 2022
"Is the a way to skip subjects? "
You can test if a particular folder of file exists:
and add an IF condition inside the loop when the file data is imported, e.g.:
if isfile(F)
S(k).data = readmatrix(F);
end
You can do something similar for the subsubfolders too, if required.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Variables 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