I need to efficiently skip lines in text file

26 次查看(过去 30 天)
I'm reading in a formatted text data file and pulling data from it. Every so often there is a break in the data and its 4 lines long. I'm reading the lines with "fgetl" and right now to skip those lines I just use
for i = 1:4
line = fgetl(fileID);
end
This seems terribly inefficient and brute force. Is there a better way?
--Clarification The skipped lines have information in them, just information I don't use. I'm also trying to avoid saving any information temporarily for the skipped lines.
  1 个评论
Cedric
Cedric 2015-8-7
编辑:Cedric 2015-8-7
Could you give an example of both types of lines and define the elimination criterion?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2015-8-7
Repeated fgetl() is going to be more efficient than what the others have shown here when you are working with text files. fgetl() is a low-level routine that talks directly to the I/O stream used by the operating system libraries.
If you were working with non-text files (that is you did not use the 't' flag when you opened them) and if you knew exactly how many bytes the 4 lines occupied in total including the line delimiters, then you could fseek() forward that many bytes.

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2015-8-7
编辑:Azzi Abdelmalek 2015-8-7
You can use textscan
fid=fopen('fic.txt');
str=textscan(fid,'%s','HeaderLines',5,'delimiter','\r\n');
fclose(fid)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by