Read file from line #1 to line #2
Show older comments
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
Accepted Answer
More Answers (1)
Fangjun Jiang
on 29 Aug 2011
fgetl() will read one line at a time. If you know which lines to read, you can write a for-loop to skip the previous lines.
Assume you want to read from line StartLine=100, for example
fid=fopen('test.txt','rt');
StartLine=100;
for k=1:StartLine-1
fgetl(fid); % read and dump
end
Fline=fgetl(fid); % this is the 100th line
%do stuff
fclose(fid)
Categories
Find more on Low-Level File I/O 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!