Is there more than 1 way to acount for header lines while using TEXTSCAN?

1 次查看(过去 30 天)
I've a text file containing the following sample data:
#*
#
#*****
# SOME TEXT
#-----
AAAA
A1D2
R2D2
C3PO
F9I4
TEST
#*****
# LOCATION
#*****
I'm extracting the character strings using the following code;
fid = fopen('Textscan_Sample_Data.txt');
Data = textscan(fid, '%s', 'HeaderLines', 5, 'CommentStyle', {'#*****', '#*****'});
fclose(fid);
The result is a 6 x 1 cell matrix containing the following;
'AAAA'
'A1D2'
'R2D2'
'C3PO'
'F9I4'
'TEST'
This works well when the number of header lines is a known, fixed value. But when the number of lines varies, the approach is no longer valid.
It doesn't appear TEXTSCAN can account for multiple comment styles. Is there a way to account for a varying number of header lines while still using the TEXTSCAN function to account for the comment style at the end of the file?

采纳的回答

Walter Roberson
Walter Roberson 2017-4-25
fid = fopen('Textscan_Sample_Data.txt');
textscan(fid, 'THIS PATTERN DOES NOT OCCUR', 1, 'CommentStyle', {'#*', '#-----'});
Data = textscan(fid, '%s', 'CommentStyle', {'#*****', '#*****'});
fclose(fid);
This uses CommentStyle to skip from the begining of file to the #----- line, leaving it positioned at the AAAA line. Then it deliberately specifies a pattern that is not present on the input to force textscan to fail at that point, leaving the stream positioned at the AAAA line. It then uses textscan again with no header but with the other CommentStyle.
  1 个评论
Brad
Brad 2017-4-25
Walter, this is quite an innovative approach. One that never crossed my mind. Thanks for taking a look at this. It appears to test out well!!

请先登录,再进行评论。

更多回答(1 个)

Gabriel Felix
Gabriel Felix 2020-5-24
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!

Translated by