Borrar filtros
Borrar filtros

Save data from subfolders into a cell array

2 visualizaciones (últimos 30 días)
Johanna Popp
Johanna Popp el 4 de Feb. de 2022
Comentada: Johanna Popp el 4 de Feb. de 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

Respuesta aceptada

Stephen23
Stephen23 el 4 de Feb. de 2022
Editada: Stephen23 el 4 de Feb. de 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 comentarios
Stephen23
Stephen23 el 4 de Feb. de 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 el 4 de Feb. de 2022
awesome, thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by