MATLAB Answers


Read only some lines of a .txt file

Asked by Adrien
on 16 May 2013


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.


  1 Comment

Are there numbers in the comments?



No products are associated with this question.

2 Answers

Answer by Sean de Wolski
on 16 May 2013

Use textscan. Throw away the lines you don't care about.


on 16 May 2013

I tried by typing the following :

fid=fopen('blabla.txt','r') ;

Data = textscan(fid, '%s')


Then Data = {289x1 cell}

Data{1} = '%%%'


'10' ... until the end of the file.

How can I just have the elements that I need?

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)?

Answer by 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.)


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!