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

2 vues (au cours des 30 derniers jours)
Claudio Savito
Claudio Savito le 19 Oct 2019
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.

Réponses (2)

Walter iacomacci
Walter iacomacci le 19 Oct 2019
Modifié(e) : Walter iacomacci le 19 Oct 2019
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 commentaire
Claudio Savito
Claudio Savito le 26 Oct 2019
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);

Connectez-vous pour commenter.


Claudio Savito
Claudio Savito le 26 Oct 2019
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);

Catégories

En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by