Reading specific lines from text file

조회 수: 7 (최근 30일)
as hz
as hz 2012년 9월 25일
Hi,
I have a text file (PCdata.txt) which I wish to read lines Xa, Xb and than lines Xa+14, Xb+14, Xa+14*2, Xb+14*2 … Xa+14*n, Xb+14*n until the end of the file.
Can some please explain me how to do it.
Cheers.

답변 (1개)

Image Analyst
Image Analyst 2012년 9월 25일
Look up the help for fread(), fgets() or fgetl(), and fclose(). Basically, open the file, with fgets() read Xa-1 lines and ignore them, then read the Xa'th line, throw away some more lines until you get to the Xb'th line and then read that, etc.
  댓글 수: 1
as hz
as hz 2012년 9월 25일
Hi,
Thanks for the help.
The solution is below if someone will need it:
fid = fopen('filename','rt');
nlines = 0;
while (fgets(fid) ~= -1),
nlines = nlines+1;
end
fclose(fid);
C = textread('filename', '%s','delimiter', '\n');
lm1=3;
lm2=5;
rowindx = 1;
while rowindx < nlines
point1=C{lm1};
point2=C{lm2};
lm1=lm1+14;
lm2=lm2+14;
rowindx = rowindx+14;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by