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

header linesMATLABmultiple comment stylestextscan

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?

Best Answer

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.