Medusa  1.1
Coordinate Free Mehless Method implementation
mm::CSV Class Reference

#include <CSV_fwd.hpp>

Detailed Description

Implements support for basic CSV I/O.

Note
To enable support for Eigen types, additional header CSV_Eigen.hpp must be included.
std::vector<double> v = {1, 2.5, -4.5, 12.34};
CSV::write("test/testdata/example.csv", v);
std::vector<double> v2 = CSV::read("test/testdata/example.csv");
// v is the same as v2
// For the following, the CSV_Eigen.hpp header needs to be included.
Eigen::MatrixXd M = CSV::readEigen("test/testdata/example.csv");
CSV::writeEigen("test/testdata/example.csv", M.transpose()*M);

Definition at line 35 of file CSV_fwd.hpp.

Static Public Member Functions

static std::vector< double > read (const std::string &filename)
 Reads a CSV file to a vector of doubles. More...
 
static std::vector< std::vector< double > > read2d (const std::string &filename, char separator=',')
 Reads a CSV file to a vector of vectors of doubles. More...
 
static Eigen::Matrix< double, -1, -1, 0, -1, -1 > readEigen (const std::string &filename, char separator=',')
 Reads a CSV file to an Eigen matrix. More...
 
template<typename arr_t >
static void write (const std::string &filename, const arr_t &array)
 Writes given array to a CSV file (as a column). More...
 
template<typename arr_t >
static void write2d (const std::string &filename, const arr_t &array, char separator=',')
 Writes given 2d array to a CSV file. More...
 
template<typename Derived >
static void writeEigen (const std::string &filename, const Eigen::MatrixBase< Derived > &expr, char separator=',')
 Writes given Eigen matrix to a CSV file. More...
 

Static Private Member Functions

static std::vector< std::string > split (const std::string &str, char separator)
 Splits given string into a vector of strings on the given separator. More...
 

Member Function Documentation

◆ read()

std::vector< double > mm::CSV::read ( const std::string &  filename)
static

Reads a CSV file to a vector of doubles.

The file must have one number per line, otherwise use CSV::read2d.

Parameters
filenamePath to the CSV file.
Exceptions
Assertionfails if file cannot be read or if it is malformed.
Examples
test/io/CSV_test.cpp.

Definition at line 10 of file CSV.cpp.

◆ read2d()

std::vector< std::vector< double > > mm::CSV::read2d ( const std::string &  filename,
char  separator = ',' 
)
static

Reads a CSV file to a vector of vectors of doubles.

Parameters
filenamePath to the CSV file.
separatorCharacter used as a separator in the CSV file.
Exceptions
Assertionfails if file cannot be read or if it is malformed.
Examples
test/io/CSV_test.cpp.

Definition at line 26 of file CSV.cpp.

◆ readEigen()

Eigen::MatrixXd mm::CSV::readEigen ( const std::string &  filename,
char  separator = ',' 
)
inlinestatic

Reads a CSV file to an Eigen matrix.

Parameters
filenamePath to the CSV file.
separatorCharacter used as a separator in the CSV file.
Exceptions
Assertionfails if file cannot be read or if it is malformed.
Note
To use this function, additional header CSV_Eigen.hpp needs to be included.
Examples
test/io/CSV_test.cpp.

Definition at line 14 of file CSV_Eigen.hpp.

◆ split()

std::vector< std::string > mm::CSV::split ( const std::string &  str,
char  separator 
)
staticprivate

Splits given string into a vector of strings on the given separator.

Example:

split("abc,def,efg", ','); // returns {"abc", "def", "efg"}

Definition at line 55 of file CSV.cpp.

◆ write()

template<typename arr_t >
void mm::CSV::write ( const std::string &  filename,
const arr_t &  array 
)
static

Writes given array to a CSV file (as a column).

Parameters
filenamePath to the CSV file.
arrayOne dimensional array of data that supports range for loop iteration.
Exceptions
Assertionfails if file cannot be written to.
Examples
test/io/CSV_test.cpp.

Definition at line 19 of file CSV.hpp.

◆ write2d()

template<typename arr_t >
void mm::CSV::write2d ( const std::string &  filename,
const arr_t &  array,
char  separator = ',' 
)
static

Writes given 2d array to a CSV file.

Parameters
filenamePath to the CSV file.
arrayTwo dimensional array of data that supports two nested range for loop iterations.
separatorCharacter to use as a separator in a CSV file.
Exceptions
Assertionfails if file cannot be written to.
Examples
test/io/CSV_test.cpp.

Definition at line 29 of file CSV.hpp.

◆ writeEigen()

template<typename Derived >
void mm::CSV::writeEigen ( const std::string &  filename,
const Eigen::MatrixBase< Derived > &  expr,
char  separator = ',' 
)
static

Writes given Eigen matrix to a CSV file.

Parameters
filenamePath to the CSV file.
exprAn Eigen expression that can be evaluated to a matrix.
separatorCharacter to use as a separator in a CSV file.
Exceptions
Assertionfails if file cannot be written to.
Examples
test/io/CSV_test.cpp.

Definition at line 44 of file CSV_Eigen.hpp.


The documentation for this class was generated from the following files:
mm::CSV::writeEigen
static void writeEigen(const std::string &filename, const Eigen::MatrixBase< Derived > &expr, char separator=',')
Writes given Eigen matrix to a CSV file.
Definition: CSV_Eigen.hpp:44
mm::CSV::readEigen
static Eigen::Matrix< double, -1, -1, 0, -1, -1 > readEigen(const std::string &filename, char separator=',')
Reads a CSV file to an Eigen matrix.
Definition: CSV_Eigen.hpp:14
mm::CSV::write
static void write(const std::string &filename, const arr_t &array)
Writes given array to a CSV file (as a column).
Definition: CSV.hpp:19
mm::CSV::split
static std::vector< std::string > split(const std::string &str, char separator)
Splits given string into a vector of strings on the given separator.
Definition: CSV.cpp:55
mm::CSV::read
static std::vector< double > read(const std::string &filename)
Reads a CSV file to a vector of doubles.
Definition: CSV.cpp:10