Effacer les filtres
Effacer les filtres

Importing multiple text files from multiple folders

2 vues (au cours des 30 derniers jours)
Madison Lowe
Madison Lowe le 26 Mar 2018
Commenté : Madison Lowe le 26 Mar 2018
I wrote the following code to read the files I need.
runs = 33137;
folders = 22;
format = '%f%C%C';
filestop = folders;
for N = 1:filestop
for n = 1:runs
foldstr = num2str(N);
indstr = num2str(n);
filename = strcat('C:\PERALS_Ra-226\b',foldstr,'\Sample226Ra(',indstr,').txt');
T = readtable(filename,'Delimiter',',','Format',format,'ReadVariableNames',false);
col = n;
A(:,col) = table2array(T(:,1:1));
fileID = fopen(filename);
tline = fgetl(fileID);
grabd = textscan(tline,'%*f%*s%{M/d/yyyy h:mm:ss a}D','delimiter',',');
stopdate(1,col) = datetime(grabd{1,1});
for M = 1:15
tline = fgetl(fileID);
end
grab = textscan(tline,'%*f%*s%d','delimiter',',');
rt(1,col) = grab{1,1};
for O = 1:15
tline = fgetl(fileID);
end
grab = textscan(tline,'%*f%*s%f','delimiter',',');
dt(1,col) = grab{1,1};
fclose(fileID);
end
end
The problem with my code is that there are a total of 33137 text files, however each folder does not have that many files. How can I change this code to read all of the files I need?

Réponse acceptée

Kai Domhardt
Kai Domhardt le 26 Mar 2018
What you want to do is check each folder individually for the number files in it.
folders = 22;
format = '%f%C%C';
filestop = folders;
for N = 1:filestop
foldername = strcat('C:\PERALS_Ra-226\b',foldstr);
listing = dir(foldername );
for n = 3:length(listing)
filename = [listing(n).folder filesep listing(n).name];
if ~listing(n).isdir && ~isempty(strfind(filename, 'Sample226Ra'))
...
The for-loop starts at 3 in order to skip the parent folder '..' and the current folder '.'.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by