To write Eigen::Matrix to file I really like to use the following:
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix_MxN;
Matrix_MxN J = Matrix_MxN::Zeros(10,10);
std::ofstream("matrix.txt") << J;
But unfortunately, something that can do the opposite is not defined:
std::ifstream("matrix.txt") >> J;
To circumvent this problem, how can you read/write an Eigen::Matrix to binary file instead?
I think there is a line missing in the
read_binary()
method. There should be anin.seekg(0, in.beg);
before the firstin.read((*char))
line.You can define these methods:
and you can test their usage with:
If you know of a better way, suggestions are welcome!
modifications to avoid intruding namespace Eigen, using zlib for bigger Matrices.