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 件のコメント

Stephen23
Stephen23 2023 年 11 月 2 日
編集済み: Stephen23 2023 年 11 月 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.
Susan
Susan 2023 年 11 月 2 日
I got it! Thank you!

サインインしてコメントする。

 採用された回答

Taylor
Taylor 2023 年 11 月 2 日

1 投票

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 件のコメント

Susan
Susan 2023 年 11 月 2 日
編集済み: Susan 2023 年 11 月 2 日
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.
Voss
Voss 2023 年 11 月 2 日
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"
Susan
Susan 2023 年 11 月 2 日
Oh my gosh!!! That's SO much easier!
Thank you!
Voss
Voss 2023 年 11 月 2 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSearch Path についてさらに検索

製品

リリース

R2023b

質問済み:

2023 年 11 月 2 日

コメント済み:

2023 年 11 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by