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

5 ビュー (過去 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 Center および File ExchangeText Files についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by