Count Total Number of Rows for Multiple .txt files in a Folder

조회 수: 8 (최근 30일)
Craig Stevens
Craig Stevens 2020년 12월 9일
댓글: Rik 2020년 12월 10일
Hi, my question should be rather simple.
I have a folder over 2000 .txt files, each of which contains post processing data in the same format as each other. Each row is equivalent to 1 second. In order to know the total length of time, I would like to run some sort of loop to just count the total number of rows cumulatively for all files. A snippet of the file can be shown below.
I appreciate the help!

채택된 답변

Rik
Rik 2020년 12월 9일
I don't think it is possible to avoid reading all files and count the number of lines in each (unless each line is a fixed length, in which case you can use the file size in bytes).
If your files are plain text you can use my readfile function, which you can get from the FEX. If you are using R2017a or later, you can also get it through the AddOn-manager. If you are using R2020b, you can also use the readlines function. You can use numel to count the number of lines. The last line might be empty, so you should probably check that.
  댓글 수: 2
Craig Stevens
Craig Stevens 2020년 12월 9일
I was trying to avoid that but I decided to just go with pulling each of them as you said, as you can see below. It certainly isn't efficient and took 12 minutes to run due to all the, but it worked for the one time I needed it. Thanks for the help.
folderName = 'C:\Users\xxxxxx';
fileInfo = dir([folderName filesep '*.txt']);
fileName={fileInfo.name}';
numberFiles = size(fileInfo(:,1));
numberFiles = numberFiles(1);
Length = zeros(1,numberFiles);
for k = 1:numberFiles;
fullFileName = [folderName filesep fileName{k}];
data = readtable(fullFileName);
Length(k) = height(data);
end
TotalLength = sum(Length);
Rik
Rik 2020년 12월 10일
numberFiles=numel(fileName) should do it in one go.
Also, if you just want to count the number of lines, you don't need to spend time parsing the data to a table.
I would also suggest using fullfile instead of this construction with filesep (fullfile will deal with trailing file separators in the folder name).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Filename Construction에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by