[SOLVED] How to read data file from a specific line ?

Hi all,
simple problem :
  1. I got a data file in which 10 first lines are characters.
  2. Then, from the line 11, there are only floats.
  3. How can I read from the line 11 until the end of the file ?
Thanks for all !
Florian

 Réponse acceptée

Hi buddy
It's dead easy, I'm copy pasting part of a code I wrote to manipulate text files in which I read the text file line by line. anytime you us
fid1=fopen('D:\Dale Jr.PLR','r'); % This opens a file
fid2=fopen('F:\NEW_Dale Jr.PLR','r');
line_num=0;
l1='0';
l2='0';
while(ischar(l1)||ischar(l2)); % this continues until the end of file
line_num=line_num+1;
l1=fgetl(fid1); % anytime you use fgetl you read a line and next time you use it reads next
l2=fgetl(fid2);
if(isequal(l1,l2)==0)
disp(['L1=',l1])
disp(['L2=',l2])
line_num
err(line_num)=line_num;
end
end
Ok in this program I checked the content of 2 files, wanted to make sure if they are the same. Ignore the program, what's important is to use fgetl ,I include some comment for you in the code, just wanna emphasis that first time you use fgetl, it reads the first line, next time you use it it will read the second line, so you don't have to have a counter for the lines, unless you want to save the lines like I did and I assigned a variable and a counter for the variable to save all the lines. Refer to MATLAB product help for more examples and information.
Good Luck!

6 commentaires

Florian
Florian le 13 Mar 2014
Modifié(e) : Florian le 13 Mar 2014
Hi !
Thank you very much for your answer, it's really fine !
Solved, Florian
Juan Espinosa
Juan Espinosa le 8 Nov 2017
Modifié(e) : Juan Espinosa le 8 Nov 2017
Hi there,
Just for clarify, if someone just want to read the content of the file
fileId=fopen('D:\MATLAB\Oscar\GUI\InterfaceC.m','r'); % This opens a file
line_num=0;
lineData='0';
while ~feof(fileId)
line_num=line_num+1;
lineData=fgetl(fileId); % read a line
disp(['Line ',num2str(line_num) , ': ' lineData ]);
end
Regards Juan
To display a file with line numbers, you can use dbtype
dbtype D:\MATLAB\Oscar\GUI\InterfaceC.m
LS
LS le 22 Jan 2021
How can I save the output lines in a vector? I would like to access every single line by an index.
For earlier releases,
LINES = regexp(fileread(FILENAME_GOES_HERE), '\r?\n', 'split');
if isempty(LINES{end}); LINES(end) = []; end %end of file correction
KAE
KAE le 13 Jan 2023
@Walter Roberson - Can't vote up your readlines suggestion, but very helpful to learn about this.

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 12 Mar 2014

0 votes

See textscan with a HeaderLines parameter of 10
Note: you cannot use csvread() or dlmread() for this purpose.
Mireia Fontanet
Mireia Fontanet le 28 Déc 2017

0 votes

Dear,
I have to txt files, one is called variables.txt and the other one is called selector.in.txt. I want to replaced the first line of variables.txt file with the second line of selector.in.txt. How can I do it?
Regards,

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by