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

19 次查看(过去 30 天)
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);

产品

Community Treasure Hunt

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

Start Hunting!

Translated by