Importing multiple text files from multiple folders

1 回表示 (過去 30 日間)
Madison Lowe
Madison Lowe 2018 年 3 月 26 日
コメント済み: Madison Lowe 2018 年 3 月 26 日
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?

採用された回答

Kai Domhardt
Kai Domhardt 2018 年 3 月 26 日
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 '.'.
  1 件のコメント
Madison Lowe
Madison Lowe 2018 年 3 月 26 日
Thank you so much!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by