Filter löschen
Filter löschen

Save data from subfolders into a cell array

2 Ansichten (letzte 30 Tage)
Johanna Popp
Johanna Popp am 4 Feb. 2022
Kommentiert: Johanna Popp am 4 Feb. 2022
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

Akzeptierte Antwort

Stephen23
Stephen23 am 4 Feb. 2022
Bearbeitet: Stephen23 am 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 Kommentare
Stephen23
Stephen23 am 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.
Johanna Popp
Johanna Popp am 4 Feb. 2022
awesome, thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by