Land data from multiple subfolders into structure

I'm trying to create a structure from multiple data files for multiple participants. When I run the following code, an empty structure is created. None of the data files loads. I'm not getting any errors. Screenshot of the files in the path is below.
I am a relatively new user, so it may be something simple.
% Create a Directory
Fold = dir('path');
SubFolds = Fold([Fold.isdir]); % keep only the directories
% Loop each folder
% For loop: Read each signal file into a data structure, then save incase we want it later
for i = 1:length(SubFolds)
data.EDA = readtable(join([Fold,'/',SubFolds(i),'/EDA.csv'],''));
data.ECG = readtable(join([Fold,'/',SubFolds(i),'/ECG.csv'],''));
data.EEG = readtable(join([Fold,'/',SubFolds(i),'/EEG.csv'],''));
data.PPG = readtable(join([Fold,'/',SubFolds(i),'/PPG.csv'],''));
data.ACC = readtable(join([Fold,'/',SubFolds(i),'/ACC.csv'],''));
disp('Participant Sructure.');
name = ['SubFolds',num2str(i)];
save(fullfile(join([Fold,'/',SubFolds(i)],''),name),'data')
end
% Lists with all of the subject Numbers and File names
parts = ["P01","P02","P03","P04","P05","P06","P07","P08","P09","P10","P11","P12","P13","P14","P15","P16","P17"];
signals = ["EDA.csv",'PPG1.csv','PPG2.csv','ECG.csv','EEG.csv'];

2 个评论

"When I run the following code, an empty structure is created"
Which structure? Do you mean that SUBFOLDS is empty? That would occur if no folders were found by DIR on the specified path. Which makes sense, because there are no folders in your screenshot named "path".
Instead of using "path", you need to use the an absolute/relative path to the main folder.

请先登录,再进行评论。

 采纳的回答

You are not calling dir properly. The string input to dir should be an absolute or relative path name. Try changing the string to 'Import_Data'.
https://www.mathworks.com/help/matlab/ref/dir.html

4 个评论

Thanks for the help. I realized what I was doing when calling the path (I hadn't included that code before) and simplified the whole thing.
For those lost souls searching discussions for answers to their own problems, this is what worked (the error is simply because I hid my actual path for this example):
% Set the Path
path = 'my_path';
parts = ["P01","P02","P03","P04","P05","P06","P07","P08","P09","P10","P11","P12","P13","P14","P15","P16","P17"];
signals = ["EDA.csv",'PPG.csv','ECG.csv','EEG.csv','DataAverage.csv'];
%%
% Loop each folder
% For loop: Read each signal file into a data structure, then save incase we want it later
for i = 1:length(parts)
data.EDA = readtable(join([path,'/',parts(i),'/EDA.csv'],''));
data.ECG = readtable(join([path,'/',parts(i),'/ECG.csv'],''));
if i ~= 2;
data.EEG = readtable(join([path,'/',parts(i),'/EEG.csv'],''));
end
data.PPG = readtable(join([path,'/',parts(i),'/PPG.csv'],''));
data.ACC = readtable(join([path,'/',parts(i),'/ACC.csv'],''));
data.BR = readtable(join([path,'/', parts(i),'/DataAverage.csv'],''));
disp('Participant Structure.');
name = ['Subject_Structure',num2str(i), '.mat'];
save(fullfile(join([path,'/',parts(i)],''),name),'data');
end
Error using readtable
Unable to find or open 'my_path/P01/EDA.csv'. Check the path and filename or file permissions.
folder = 'my_path';
parts = "P01";
i = 1;
@Susan Tilbury: You can use fullfile to construct your file names, which allows you to avoid having to type the directory separator character (in this case '/') that you had to have when using join:
join([folder,'/',parts(i),'/EDA.csv'],'')
ans = "my_path/P01/EDA.csv"
fullfile(folder,parts(i),'EDA.csv') % the same, but clearer
ans = "my_path/P01/EDA.csv"
Also, you can simplify this part and avoid having to type all that:
parts = ["P01","P02","P03","P04","P05","P06","P07","P08","P09","P10","P11","P12","P13","P14","P15","P16","P17"]
parts = 1×17 string array
"P01" "P02" "P03" "P04" "P05" "P06" "P07" "P08" "P09" "P10" "P11" "P12" "P13" "P14" "P15" "P16" "P17"
parts = compose("P%02d",1:17) % the same, but easier
parts = 1×17 string array
"P01" "P02" "P03" "P04" "P05" "P06" "P07" "P08" "P09" "P10" "P11" "P12" "P13" "P14" "P15" "P16" "P17"
Oh my gosh!!! That's SO much easier!
Thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Search Path 的更多信息

产品

版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by