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 commentaires

Stephen23
Stephen23 le 2 Nov 2023
Modifié(e) : Stephen23 le 2 Nov 2023
"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.
I got it! Thank you!

Connectez-vous pour commenter.

 Réponse acceptée

Taylor
Taylor le 2 Nov 2023

1 vote

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 commentaires

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!
Voss
Voss le 2 Nov 2023
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by