Clear Filters
Clear Filters

Import data into matlab but ignore subject name

4 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 1 Apr 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

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!