HyperMath

Reading General ASCII Files

Reading General ASCII Files

Previous topic Next topic Expand/collapse all hidden text  

Reading General ASCII Files

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

M, t = FReadText(fid,[delim],[start])

Reads the header and numeric sections of a formatted text file separately. The header section (optional) is returned as a table and the numeric section is stored in a matrix. The header section (if any) must precede the numeric section. The start line number of the numeric section can be specified, as well as the delimiters separating each column of the numeric section. Leading spaces and tabs in a column are ignored if they are not specified as the delimiter. Trailing spaces & tabs cause the rest of the column entries to be ignored if they are not specified as the delimiter. Non-numeric entries in the numeric section will halt the read, discard the offending line and produce an error.

If the number of columns is not the same for each row of the numeric section, then the output matrix will assume the highest number of columns present and fill in the voids with zeros. In case no numeric section is found an empty matrix is returned. Similarly if no header section is present an empty table is returned.

fid

Handle to an opened text file.

delim

The delimiters separating each column of the numeric data section.  Defaults to space.  Tabs are specified by ‘\t’.  A single delimiter can be specified by as a string.  If there are multiple delimiters, then they can be specified as a table array of strings (the order is unimportant).

start

The starting line number of the numeric section in the file.  Defaults to 1 (no header section).

M

A matrix of the numeric section. The rows correspond to the ones in the numeric section.

t

A table of the header section. Each row in the header section is stored as a single string.

hmtoggle_plus1Example

Consider the file test.txt with content as shown below:

Date XX/XX/XXXX

Item1,Item2,Item3

1,2,3

4,5,6

This can be read as follows:

fid = Open("c:/test.txt","r")

M,t = FReadText(fid, ',', 3) // start reading numeric section from line 3

Close(fid)

The results will be:

M (2 x 3 matrix):

1 2 3

4 5 6

t (table of 2 entries)

t[1] = {‘Date XX/XX/XXXX’}

t[2] = {‘Item1,Item2,Item3’}