Is there more than 1 way to acount for header lines while using TEXTSCAN?
Show older comments
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?
Accepted Answer
More Answers (1)
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};
Categories
Find more on String Parsing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!