Clear Filters
Clear Filters

Load one file from many different folders

1 view (last 30 days)
I have an huge amount of data. I want to load into matlab.
The data is placed in many different folders. But each with one .dat file.
Are there a clever way to do this?
P = 'C:\UndrianedMon\triax_hypoplasticityta70-print-out';
S = dir(fullfile(P,'EALL_element_1.dat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = importdata(F).data;
end
This code works for me. But is not that smart, as i need to copy it many times to get the .dat files I need.
Can anyone help me?

Accepted Answer

Voss
Voss on 6 Mar 2024
You can use wildcard characters in dir to find files in many different folders.
Examples:
% some directory that contains all folders containing your dat files:
P = 'C:\UndrainedMon';
% (1) this gets info about all .dat files in any folder in P
% (i.e., only one level down from P):
S = dir(fullfile(P,'*','*.dat'));
% (2) this gets info about all .dat files in any folder anywhere in P
% (i.e., any number of levels down from P):
S = dir(fullfile(P,'**','*.dat'));

More Answers (0)

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!