MATLAB: Read file from line #1 to line #2

read line number

Hi,
I'd like to know hot to read a text file between certain line numbers. It's a huge file and I only need to use a portion of it.
Thanks in advance,
Ed

Best Answer

You cannot tell fgetl() which line to start reading from.
If you have read past a section of file and want to go back, then you need to use fseek(). Note though that fseek() cannot seek by line number. You may need to fseek() to the beginning of the file and then use a loop to read and discard a line at a time.
When you are reading in the file the first time, if you can detect (e.g., with a counter) that you are at a place you are going to want to come back to later, you can use ftell() to record the location in a way that fseek() knows how to use to go back to that exact spot.
Related Question