How can I read a specific line from a data file (.txt)?

조회 수: 9 (최근 30일)
Claudio Savito
Claudio Savito 2019년 10월 19일
답변: Claudio Savito 2019년 10월 26일
How Can I read a specific line from a data file (.txt)?
Hi everyone,
simple problem :
I got a data file with a much lines (characters and numbers).
How can I read from the line 30 of the file (the line where appears "INACTIVE_LENGTH = 721.8400 " ?
I've just seen a lot of answers about this topic, but I didn't find a right answer for my problem.
Thanks for all !
Claudio
P.S. the data file in question is attached. Its original format is *.trk, but it's well readable by Notepad, Blocconote, etc.

답변 (2개)

Walter iacomacci
Walter iacomacci 2019년 10월 19일
편집: Walter iacomacci 2019년 10월 19일
Hi Claudio this Question has been already answered before here: Read an Specific line from a .txt, I think you will find your solution there!.
You are welcome.
Walter A. Iacomacci
  댓글 수: 1
Claudio Savito
Claudio Savito 2019년 10월 26일
Thanks Walter,
I tried to run the code that you has suggested me, but it gives me errors.
I wrote:
% open the file
fid=fopen('E:\Claudio.cdb\tracks.tbl\MF011\F011_20.txt','r');
% set linenum to the desired line number that you want to import
linenum = 30;
% use '%s' if you want to read in the entire line or use '%f' if you want to read only the first numeric value
param1 = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
Matlab gives me in return fid = -1, and these errors:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Untitled2 (line 127)
param1 = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);

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


Claudio Savito
Claudio Savito 2019년 10월 26일
Now, I tried:
% open the file
fid=fopen('E:\Claudio.cdb\tracks.tbl\MF011\MF011_20.txt','w','n');
%%
% set linenum to the desired line number that you want to import
linenum = 30;
% use '%s' if you want to read in the entire line or use '%f' if you want to read only the first numeric value
param1 = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
Now fopen works and gives me fid=-6, but it appears an error in textscan:
Error using textscan
Unable to read any data from the file. TEXTSCAN might not have read access.
Error in Untitled2 (line 131)
param1 = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by