Read only some lines of a .txt file
1 view (last 30 days)
Show older comments
Hi,
I have a .txt file looking like this :
%%%comment <br /> 10 <br /> %%%comment <br /> 20 <br /> %%%comment <br /> 30
(each elemnt on a different line, hence 6 lines)
and I need that Matlab read only the lines 2, 4 and 6 (the numbers in fact).
How can I do for that it do not read the lines that I do not want that he read?
Thank you for your answer.
AC
Answers (2)
Walter Roberson
on 16 May 2013
There is no way to not read the lines you do not want to read. This gets down to underlying operating system representations of files.
Files in OS-X, Linux, and MS Windows, are simply streams of bytes with a line terminator character between the lines. (The line terminator is linefeed in general, or carriage return followed by linefeed in older MS Windows programs.) The operating systems do not keep any kind of internal record of where the lines begin or end. The only way that the operating system has of reading a line is to proceed character by character until it finds the line terminator. Thus if you want to read a particular line number, it is necessary to read in all of the previous lines.
The rules start to change if you have information about exactly how far into the file some particular information is. You seldom have that information for text files because text lines are variable length. If you did somehow have that information then the fseek() operation could be used to position to the known location. That might still involve internally reading a portion of the file, but if the jump is big enough (over about 1 Kb) then the operating system does not need to look at all the contents in the middle. (Imagine there was a fixed size of information in each of a number of envelopes: you could count envelopes until you got to the one you needed instead of having to examine the contents of each envelope.)
0 Comments
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!