How can a Eigen matrix be written to file in CSV format?
csv, eigen, file-io, matrix
Solution
Using `format` is a bit more concise:
// define the format you want, you only need one instance of this...
const static IOFormat CSVFormat(StreamPrecision, DontAlignCols, ", ", "\n");
...
void writeToCSVfile(string name, MatrixXd matrix)
{
ofstream file(name.c_str());
file << matrix.format(CSVFormat);
}
Problem
Suppose I have a double Eigen matrix and I want to write it to a csv file. I find the way of writing into a file in raw format but I need commas between entries. Here is the code I foudn for simple writing. ``` void writeToCSVfile(string name, MatrixXd matrix) { ofstream file(name.c_str()); if (file.is_open()) { file << matrix << '\n'; //file << "m" << '\n' << colm(matrix) << '\n'; } } ```