Load .dat files from subfolders with different names

7 views (last 30 days)
Hello everyone,
I have Folder1 which contains a number of subfolders with different names (subfolder_1,subfolder_th etc). Each subfolder has 7 .mat files with the same name (data1,data2,....data7). So i've got two questions:
1. Is it feasible somehow to open one subfolder at a time in a for loop in order to process tha data?
2. Is there a way to load for example data1 from all subfolders and then do the same thing for the other data?
*i guess the main problem is the subfolder's name detection and storing.
  4 Comments
Stephen23
Stephen23 on 20 May 2021
Edited: Stephen23 on 20 May 2021
"but I don't see a way to make matlab read each file in each subfolder."
ignacio bobadilla tapia
ignacio bobadilla tapia on 20 May 2021
This I have so far, I don't know how to start a loop correctly so that it reads the files of interest to me.
Base = 'D:\Date';
List = dir(fullfile(Base, '*.*));
List = List([List.isdir]);
SubFolder = {List.name};
SubFolder(ismember(SubFolder, {'.', '..'})) = [];

Sign in to comment.

Answers (2)

Stephen23
Stephen23 on 20 May 2021
Edited: Stephen23 on 20 May 2021
This should get you started. Adapt to suit your data files, folder names, etc.:
P = 'absolute/relative path to where the files are saved';
n1 = 19; % number of sub-directories
n2 = 7; % number of mat-files.
C = cell(n1,n2);
for k1 = 1:n1
D = sprintf('subfolder_%d',k1);
for k2 = 1:n2
F = sprintf('data%d.mat',k2);
C{k1,k2} = load(fullfile(P,D,F));
end
end
  2 Comments
Stephen23
Stephen23 on 20 May 2021
Edited: Stephen23 on 20 May 2021
"this code does not work for me."
I cannot help debug your code if you do not show what you tried and explain what happened when you ran it.
Did the code run without error? Did you get some results, but not the results you expected? Did the code throw any warnings? Did the code throw an error (if so, what is the complete error message?) Did the code run but not return anything? How did you check this?
If you tell us nothing, we cannot help you debug. If you tell us nothing, it slows down your work.

Sign in to comment.


per isakson
per isakson on 20 May 2021
Edited: per isakson on 20 May 2021
Yes to both questions. With help of the code snippets below I guess you can put together a script.
sad = dir( fullfile('Folder1','**','data*.mat') ); % assuming the names starts with "data"
ffs_list = fullfile( {sad.folder}, {sad.name} ); % full file specifier
for cac = ffs_list % cac is a scalar cell array
ffs = cac{1};
% do stuff
end
or maybe better in you case
sad = dir( fullfile('Folder1','**','data*.mat') ); % sad is a 1D struc array
for jj = 1 : length(sad)
sad(jj).name
sad(jj).folder
% do stuff
end
"I guess the main problem is the subfolder's name detection" The '**' in the dir-statement does that.
The statements
isa = ismember( {sad.name}, 'data1.mat' );
sad_data1 = sad( isa );
creates a subset containing all the files named, 'data1.mat'
The statements (assuming that the subfolder names are legal Matlab names)
cac=regexp( string({sad.folder}), '(?<=\\)\w+$', 'match' );
unique_subfolder_names = unique( horzcat( cac{:} ) );
creates a string array of unique subfolder names. To sort this array you might need Natural-Order Filename Sort by @Stephen Cobeldick
  2 Comments
Stephen23
Stephen23 on 20 May 2021
Edited: Stephen23 on 20 May 2021
Using the recursive search is a good idea, it also makes storing the data easy in the same structure:
sad(jj).data = ... whatever
Note that using DIR by itself will not return folder names subfolder 1 ... 19 in alphanumeric order.
per isakson
per isakson on 20 May 2021
"Note that using DIR by itself will not return folder names subfolder 1 ... 19 in" indeed it does not.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!