Import data into matlab but ignore subject name

1 visualización (últimos 30 días)
Johanna Popp
Johanna Popp el 1 de Abr. de 2022
Comentada: Johanna Popp el 1 de Abr. de 2022
Hi all,
I've been using this code to import data from my directory into a matlab structure. Data is saved in a way that each csv-file is saved in a subfolder that is named according to the subject ID. It has always worked quite well because the files in the subfolders were named identically, but now I ran into a problem since the names of the files that I am wanting to import (' the name of the file that you want.csv') include the subject name (before all were named data_matrix.csv but now they are named subject_1_data_matrix.csv, subject_2_data_matrix.csv etc.). Is there an easy way to ignore the subject-specific information in the name of the data? Maybe a placeholder?
Thanks!
Johanna
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

Respuesta aceptada

Stephen23
Stephen23 el 1 de Abr. de 2022
P = 'absolute or relative path to where the subfolders are';
S = dir(fullfile(P,'**','*data_matrix.CSV'));
for k = 1:numel(S) % loop over the files
F = fullfile(S(k).folder,S(k).name);
S(k).data = readmatrix(F);
end

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by