Read only some lines of a .txt file

1 view (last 30 days)
Adrien
Adrien on 16 May 2013
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)

Sean de Wolski
Sean de Wolski on 16 May 2013
Use textscan. Throw away the lines you don't care about.
  2 Comments
Adrien
Adrien on 16 May 2013
I tried by typing the following :
fid=fopen('blabla.txt','r') ;
Data = textscan(fid, '%s')
fclose(fid);
Then Data = {289x1 cell}
Data{1} = '%%%'
'comment'
'10' ... until the end of the file.
How can I just have the elements that I need?
Adrien
Adrien on 16 May 2013
OK finally i found : Data{1}(n) gives the n-th element, so it's ok on this point.
However, I have in fact far more lines than what I wrote, then do I have to write the format for all of the elements in the file (including all the ones I don't need) or is there a shorter way (knowing that there are both string and integers that I want to pick from the txt file)?

Sign in to comment.


Walter Roberson
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.)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!