read a matrix from a text file into Eigen matrix

#include <Eigen/Dense>
#include <Eigen/Dense>
#include <vector>
#include <fstream>
#include <igl/list_to_matrix.h>
using namespace std;
void read_matrix(std::string fileName, Eigen::MatrixXd &outputMat) {
fstream cin;
cin.open(fileName.c_str());
if (cin.fail())
{
std::cerr << "Failed to open file: " << fileName << std::endl;
std::cin.get(); }
string s;
vector <vector <double> > matrix;
while (getline(cin, s)) {
stringstream input(s);
double temp;
vector <double> currentLine;
while (input >> temp)
currentLine.push_back(temp);
matrix.push_back(currentLine);
}
if (!igl::list_to_matrix(matrix, outputMat))
{ std::cerr << "list tom matrix error" << std::endl; std::cin.get();
//return false;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s