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

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

댓글을 달려면 로그인하십시오.

 채택된 답변

José-Luis
José-Luis 2012년 11월 12일

0 개 추천

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

Hi,
As you suggested, 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 only:
[Number, time(16215.00), x, y, z]
Thanks, Rami
Jan
Jan 2012년 11월 12일
Use a matching SSCANF command.
To read two header lines, you need two FGETS commands. Afterwards TEXTSCAN should not skip the header lines again, because this would ignore two lines of data.
Rami
Rami 2012년 11월 12일
Thanks Jan,
Your comment was useful for me. However I was not able to make work SSCANF, but STRFIND and STR2DOUBLE.
Thanks for taking time to help. Rami
Dr. Seis
Dr. Seis 2012년 11월 12일
편집: Dr. Seis 2012년 11월 12일
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 2012년 11월 12일
Thanks Elige,
That was an elegant way to get the number.
Rami

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Gabriel Felix
Gabriel Felix 2020년 5월 24일

0 개 추천

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};

카테고리

질문:

2012년 11월 12일

답변:

2020년 5월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by