필터 지우기
필터 지우기

Import data into matlab but ignore subject name

조회 수: 4 (최근 30일)
Johanna Popp
Johanna Popp 2022년 4월 1일
댓글: Johanna Popp 2022년 4월 1일
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

채택된 답변

Stephen23
Stephen23 2022년 4월 1일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by