Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Eigen library's Matrix or Vector types to string?

Tags:

eigen

eigen3

How to get text representation for Vector3f or other types in Eigen library. I see lot of examples that uses .format() which returns WithFormat class. This then can be used with cout. However I'm looking for way to get Vector3f as std:string in some human readable form. Exact formatting isn't too important so if Eigen has any default formatting then that works as well.

Note: I can certainly use stringstream to replace cout but I'm hopping there is more direct way to do this.

like image 768
Shital Shah Avatar asked May 29 '16 09:05

Shital Shah


2 Answers

The easiest solution I found out, not sure if optimal, is:

static std::string toString(const Eigen::MatrixXd& mat){
    std::stringstream ss;
    ss << mat;
    return ss.str();
}
like image 119
Serk Avatar answered Jan 04 '23 10:01

Serk


As far as I know, the stringstream method is the way to go. In fact, in the IO.h file (Eigen::internal::print_matrix) the developers use stringstreams to obtain the width of each entry.

like image 26
Avi Ginsburg Avatar answered Jan 04 '23 10:01

Avi Ginsburg