Import data into matlab but ignore subject name
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!