FGETL taking a long time to execute
조회 수: 7 (최근 30일)
이전 댓글 표시
I am running fgetl in a loop similar to the code below. When profiling the code, I noticed that fgetl(fid) takes the most time to execute compared to the rest of the code. After checking the fgetl documentation, I found the following note in the documentation:
fgetl is intended for use with files that contain newline characters. Given a file with no newline characters, fgetl may take a long time to execute.
However, the documentation does not provide a solution for files without newline characters. Since my file lacks newline characters, what would be the best alternative to fgetl?
fid = fopen(FileName);
while ischar(tline) %loops over 1e5 times
% read new line
tic
tline = fgetl(fid);
toc
% Do stuff with tline
end
댓글 수: 0
채택된 답변
Matt J
2025년 1월 30일
댓글 수: 5
Walter Roberson
2025년 1월 30일
You could experiment with using
cell2mat(textscan(FID, '%[^\n]', BUFFERSIZE))
to read BUFFERSIZE lines. It might potentially be faster than calling fgetl() BUFFERSIZE times, due to reduced overhead.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!