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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Read only some lines of a .txt file

Asked by 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

1 Comment

Cedric Wannaz on 16 May 2013

Are there numbers in the comments?

Adrien

Tags

Products

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.

2 Comments

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

Sean de Wolski
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.)

0 Comments

Walter Roberson

Contact us