reading header line from text file

62 views (last 30 days)
Rami
Rami on 12 Nov 2012
Answered: Gabriel Felix on 24 May 2020
Hi,
I have text file with 2 header lines and data (7 columns). I can read the data using textscan, but not able to read header file (using fgets). Please see the code below:
fid = fopen(files(1).name, 'rt');
C = textscan(fid, '%f %f %f %f %f %f %f' ,'HeaderLines',2,'Delimiter',',');
tline = fgets(fid);
fclose(fid);
It gave tline = -1.
Any help regarding the problem, will be really appreciated. Thanks, Rami
  1 Comment
Rami
Rami on 12 Nov 2012
Hi,
I able to resolve the problem with writing fgets first and then textscan:
fid = fopen(files(1).name, 'rt');
tline = fgets(fid);disp(tline)
C = textscan(fid, '%f %f %f %f %f %f %f ' ,'HeaderLines',2,'Delimiter',',');
fclose(fid);
But now I run to another problem, how can I read the particular text from header line. For example, from header line below, I want to read number:
[Number, time(16215.00), x, y, z]
Thanks, Rami

Sign in to comment.

Accepted Answer

José-Luis
José-Luis on 12 Nov 2012
It means the position indicator is not at the beginning of the file after it exits textscan:
You can either rewind:
fid = fopen(files(1).name, 'rt');
C = textscan(fid, '%f %f %f %f %f %f %f' ,'HeaderLines',2,'Delimiter',',');
frewind(fid);
tline = fgets(fid);
fclose(fid);
Or read it first:
fid = fopen(files(1).name, 'rt');
line1 = fgets(fid);
line2 = fgets(fid);
C = textscan(fid, '%f %f %f %f %f %f %f' ,'Delimiter',',');
tline = fgets(fid);
fclose(fid);
  5 Comments
Dr. Seis
Dr. Seis on 12 Nov 2012
Edited: Dr. Seis on 12 Nov 2012
If you always have the same "somethings" then you may be able to do something like this:
headerline = '[something,something(546231122.23),x,y,z]';
number = sscanf(headerline,'[something,something(%f),x,y,z]');
Rami
Rami on 12 Nov 2012
Thanks Elige,
That was an elegant way to get the number.
Rami

Sign in to comment.

More Answers (1)

Gabriel Felix
Gabriel Felix on 24 May 2020
I had to use \n at the end of each line. Without it I couldn't make textscan() work properly, even thoug the "HeaderLines" was configured according to the text file lines. This was the only solution I found after struggling with the code for an intire day.
This was the text:
!
!
! alfa (graus) = 5.0
!
! Id. x/s z/s alfai cl c*cl/cmed cdi cmc/4
! (graus)
1 .246 .050 -1.209 .255 .332 .00538 .0170
2 .292 .150 -1.098 .259 .319 .00496 .0545
3 .339 .250 -.925 .254 .297 .00410 .0944
4 .385 .350 -.741 .243 .268 .00315 .1341
5 .432 .450 -.561 .227 .235 .00223 .1714
6 .479 .550 -.393 .206 .199 .00141 .2034
7 .525 .650 -.238 .181 .163 .00075 .2266
8 .572 .750 -.101 .152 .126 .00027 .2362
9 .619 .850 .014 .116 .089 -.00003 .2236
10 .659 .938 .103 .074 .052 -.00013 .1693
!
! CL asa = .208
! CDi asa = .00258
! e (%) = 88.9
! CMc/4 asa = .1339
My code:
%! alfa (graus) = 5.0
P = textscan(fid,'! alfa (graus) = %f','Delimiter',' ','MultipleDelimsAsOne',true,'headerLines',2,'CollectOutput',1);
alpha(1) = P{1};
%! CL asa = .208
P = textscan(fid,'! CL asa = %f\n','Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',1,'headerLines',4+n);
CL(1) = P{1};
%! CDi asa = .00258
P = textscan(fid,'! CDi asa = %f\n','Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',1,'headerlines',0);
CDi(1) = P{1};
%! CMc/4 asa = .1339
P = textscan(fid,'! CMc/4 asa = %f','Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',1,'HeaderLines',2);
Cmc4(1) = P{1};

Community Treasure Hunt

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

Start Hunting!