22 #include <vnl/vnl_matrix.h> 34 const mwSize* dims = mxGetDimensions(pr);
35 int row =
static_cast<int>(dims[0]);
36 int column =
static_cast<int>(dims[1]);
38 double * data = mxGetPr(pr);
39 if (mat->rows()!=row || mat->columns()!=column )
40 mat->set_size(row, column);
42 for (
int i = 0; i < row; i += 1 )
43 for (
int j = 0; j < column; j += 1 )
44 (*mat)[i][j] = data[j*row+i];
51 int row = mat->rows();
52 int column = mat->columns();
55 double * data = mxGetPr(pr);
56 for (
int i = 0; i < row; i += 1 )
57 for (
int j = 0; j < column; j += 1 )
58 data[j*row+i] = (*mat)[i][j];
65 const mwSize* dims = mxGetDimensions(pr);
66 int row =
static_cast<int>(dims[0]);
67 int column =
static_cast<int>(dims[1]);
69 utlException(row>1 && column>1,
"mxArray is not a vector");
71 double * data = mxGetPr(pr);
72 if (mat->size()!=size )
75 for (
int i = 0; i < size; i += 1 )
83 int row = mat->size();
86 double * data = mxGetPr(pr);
87 for (
int i = 0; i < row; i += 1 )
95 SaveMatrixToMatFile<vnl_matrix<T> >(*mat, mat->rows(), mat->columns(), fileName, varibleName);
103 pmat = matOpen(fileName.c_str(),
"r");
107 mxArray *pr= matGetVariable(pmat, varibleName.c_str());
109 const mwSize* dims = mxGetDimensions(pr);
110 int row =
static_cast<int>(dims[0]);
111 int column =
static_cast<int>(dims[1]);
113 if (mat->rows()!=row || mat->columns()!=column )
114 mat->set_size(row, column);
116 double * data = (
double*)mxGetData(pr);
117 for (
int i = 0; i < row; i += 1 )
118 for (
int j = 0; j < column; j += 1 )
119 (*mat)(i,j) = data[j*row+i];
void ReadMatFileToVNLMatrix(const std::string fileName, const std::string varibleName, vnl_matrix< T > *mat)
#define utlException(cond, expout)
void GetVNLMatrixFromMXArray(const mxArray *pr, vnl_matrix< T > *mat)
mxArray * CreateMatrix< double >(int m, int n)
Create a m x n double matrix.
const T & max(const T &a, const T &b)
Return the maximum between a and b.
void GetMXArrayFromVNLVector(const vnl_vector< T > *mat, mxArray *&pr)
#define utlGlobalException(cond, expout)
Contains miscellaneous functions for mex files. utl functions for mex code. Some codes are from spams...
void GetVNLVectorFromMXArray(const mxArray *pr, vnl_vector< T > *mat)
void GetMXArrayFromVNLMatrix(const vnl_matrix< T > *mat, mxArray *&pr)
void SaveVNLMatrixToMatFile(const vnl_matrix< T > *mat, const std::string fileName, const std::string varibleName)