Importing multiple text files from multiple folders
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 '.'.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!