From: Brendan on
Suppose I had a text file (A.txt) I wanted to load into MATLAB.

#OK
#SQL= Select *
# FROM A..B
#
#MAXROWS UNLIMITED
#QUERYTIMEOUT 420 sec
#DATE 2010-02-04 15:25:27.990 CET
#QUERYTIME 3 millisec
#COLUMN 1 name=haloId JDBC_TYPE=-5 JDBC_TYPENAME=bigint
#COLUMN 2 name=subhaloId JDBC_TYPE=-5 JDBC_TYPENAME=bigint
#COLUMN 3 name=fofId JDBC_TYPE=-5 JDBC_TYPENAME=bigint
#COLUMN 4 name=treeRootId JDBC_TYPE=-5 JDBC_TYPENAME=bigint
#COLUMN 5 name=descendantId JDBC_TYPE=-5 JDBC_TYPENAME=bigint
#COLUMN 6 name=lastProgenitorId JDBC_TYPE=-5 JDBC_TYPENAME=bigint
#COLUMN 7 name=mainLeafId JDBC_TYPE=-5 JDBC_TYPENAME=bigint
column 1, column 2, column 3, column 4
1 , 2 , 3, 4
4, 5, 2, 3
etc. etc.


To turn my data file into an array I am using

nheaderlines = 16
format = '%n,%n,%n,'
fid = fopen('./data/A.txt','r');
AqF2_MP = textscan(fid, format,'headerlines',nheaderlines);
fclose(fid);

Now my question - how can I determine how many rows begin with a # so I can automatically use that as the number of headerlines to skip. The idea is when I load another txt file (B.txt) it might have 20 header lines I want to remove and so I have to manually change nheaderlines to 20. Is there a way of automatically determining this so it can load an arbitrary txt file excluding n headerlines 'on the fly'. Hope this makes sense! Thanks!
From: Leslie McBrayer on

> Now my question - how can I determine how many rows begin with a # so I
> can automatically use that as the number of headerlines to skip. The idea
> is when I load another txt file (B.txt) it might have 20 header lines I
> want to remove and so I have to manually change nheaderlines to 20. Is
> there a way of automatically determining this so it can load an arbitrary
> txt file excluding n headerlines 'on the fly'. Hope this makes sense!
> Thanks!
>

If all of the files look like your sample, in the sense that there are a
varying number of lines that start with #, but only one line of "column
headers", how about using the 'CommentStyle' parameter?

For example:

AqF2_MP = textscan(fid, format,'commentstyle','#','headerlines',1);


 | 
Pages: 1
Prev: size differ
Next: import text file in cell