ASL
Loading...
Searching...
No Matches
TabularDataFile Class Reference

Detailed Description

This class allows reading/writing CSV files and writing ARFF files.

Files have an optional header with column names, and data rows that can contain numbers (integer or floating point) or strings. When reading, the class tries to infer if there is a header and what separator and decimal symbol are used.

A data row is written when it has as many elements as the header indicates. Make sure you match written data to specified columns!

TabularDataFile file("data.csv");
file.columns("i,x,cos,sign");
for(int i=0; i<100; i++)
{
float x = 0.1*i, y = cos(x);
file << i << x << y << ((y<0)? "neg" : "pos");
}
An Array is a contiguous and resizable array of any type of elements.
Definition Array.h:69
This class allows reading/writing CSV files and writing ARFF files.
Definition TabularDataFile.h:77

Reading the file would be done as follows. This class will try to detect the separator used in the read file (if ',', ';' or 'tab'), and will try to detect if there is a header line (when no numbers are found in it).

TabularDataFile file("data.csv");
while(file.nextRow())
{
int i = file[0]; // Note you can use field indices
float x = file["x"]; // or column names
float y = file["sin"];
}
String represents a character string that behaves similarly to JavaScript strings in that it can be c...
Definition String.h:126

Or just as:

If a file uses a specific format that autodetection can't handle, use readAs() to sepecify column types. That includes the ability to read numbers as hexadecimal. For example:

file.readAs("hsn");

will interpret a row like 20,20,20 as hexadecimal 0x20 (32), string "20" and number 20.0.

For writing ARFF files, just set a file name with a .arff extension and specify columns with optional types (default is numeric). For nominal types, write classes separated with |, for strings use 's':

TabularDataFile file("data.arff"); // relation name will be "data"
file.columns("i,x,cos,sign:neg|pos,name:s");

#include <TabularDataFile.h>

Public Member Functions

 TabularDataFile (const String &filename)
 Creates a data file with name filename for reading or writing.
 
 TabularDataFile (const String &filename, const Array< String > &cols)
 Creates a data file with name filename for writing with the given column names.
 
TabularDataFilecolumns (const String &cols)
 Defines the columns for the data with a comma separated list of column names.
 
TabularDataFilecolumns (const Array< String > &cols)
 Defines the columns for the data.
 
int numColumns () const
 Returns the number of columns.
 
const Array< String > & columns ()
 Returns the column names.
 
void setSeparator (char s)
 Set field separator (',' by default)
 
void setDecimal (char d)
 Set decimal separator (dot by default)
 
void useQuotes ()
 Makes string values be quoted when writing.
 
void flushEvery (int nrows)
 Forces a real disk write every nrows number of rows.
 
void readAs (const String &types)
 Sets the types to read for each column as a string with chars: i:int, n:number, h:hex, s:string.
 
TabularDataFileoperator<< (const Var &x)
 Appends one data item to the current row, and writes the row if it reaches the number of columns defined.
 
Array< Array< Var > > data ()
 Returns the whole file contents as a matrix (array of arrays)
 
bool nextRow ()
 Reads the next row and returns true if it succeded.
 
Var operator[] (int i) const
 Returns the value for column index i in the current row.
 
Var operator[] (const String &col) const
 Returns the value for column named col in the current row.
 

The documentation for this class was generated from the following file: