32 template<
typename Scalar,
int RowsAtCompileTime,
int ColsAtCompileTime>
33 inline bool loadMatrix(std::string filename, Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime>& m)
41 std::ifstream input(filename.c_str());
44 std::cerr <<
"ERROR. Cannot find file '" << filename <<
"'." << std::endl;
45 m = Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime>(0,0);
51 std::vector<Scalar> v;
53 while (getline(input, line))
56 std::stringstream input_line(line);
57 while (!input_line.eof())
65 int n_cols = v.size()/n_rows;
66 m = Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime>(n_rows,n_cols);
68 for (
int i=0; i<n_rows; i++)
69 for (
int j=0; j<n_cols; j++)
70 m(i,j) = v[i*n_cols + j];
82 template<
typename Scalar,
int RowsAtCompileTime,
int ColsAtCompileTime>
83 inline bool saveMatrix(std::string directory, std::string filename, Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> matrix,
bool overwrite)
85 if (directory.empty())
89 if (!boost::filesystem::exists(directory))
92 if (!boost::filesystem::create_directories(directory))
94 std::cerr <<
"Couldn't make directory '" << directory <<
"'. Not saving data." << std::endl;
99 filename = directory+
"/"+filename;
111 template<
typename Scalar,
int RowsAtCompileTime,
int ColsAtCompileTime>
112 inline bool saveMatrix(std::string filename, Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> matrix,
bool overwrite)
114 if (boost::filesystem::exists(filename))
119 std::cerr <<
"File '" << filename <<
"' already exists. Not saving data." << std::endl;
126 file.open(filename.c_str());
129 std::cerr <<
"Couldn't open file '" << filename <<
"' for writing." << std::endl;
bool saveMatrix(std::string directory, std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > matrix, bool overwrite)
Save an Eigen matrix to an ASCII file.
bool loadMatrix(std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > &m)
Load an Eigen matrix from an ASCII file.