/
matlab.h
61 lines (46 loc) · 1.22 KB
/
matlab.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Dale Roberts <dale.o.roberts@gmail.com>
//
#ifndef volmodels_matlab_h
#define volmodels_matlab_h
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/numeric/ublas/matrix.hpp>
bool ReadMatlabMatrix(const std::string &filename, boost::numeric::ublas::matrix<double> &m )
{
using namespace std;
ifstream infile(filename);
vector<double> values;
if ( infile.bad() )
{
std::cerr << "Error:" << __FUNCTION__ << ":"
<< "Cannot open file : " << filename << "\n";
return false;
}
string line;
int linecount = 0;
while (getline(infile, line))
{
istringstream linestream(line);
string item;
double value;
while (getline(linestream, item, ','))
{
istringstream itemstream(item);
itemstream >> value;
values.push_back(value);
}
linecount++;
}
int rows = linecount;
int cols = values.size() / rows;
m.resize(rows, cols);
int k = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
m(i,j) = values[k++];
return true;
}
#endif