1 #ifndef __MATLABINTERFACE_HH__
2 #define __MATLABINTERFACE_HH__
74 #include <bias_config.h>
78 #ifdef BIAS_HAVE_OPENMP
79 # error MATLAB cannot load OpenMP-lib, please re-configure with USE_OPENMP=OFF
80 #endif //BIAS_HAVE_OPENMP
83 #ifndef BIAS_HAVE_MATLAB
84 # error MATLAB mex-headers are needed, please re-configure with USE_MATLAB=ON
85 #endif //BIAS_HAVE_MATLAB
91 #include <Base/Math/Vector.hh>
92 #include <Base/Math/Matrix.hh>
95 #define MATLABERR(x) mexErrMsgTxt(x)
98 #define MATLABWARN(x) mexWarnMsgTxt(x)
109 BIASERR(
"Given mxArray-pointer is NULL!");
114 if (!mxIsDouble(mxVec) || mxIsComplex(mxVec)) {
115 BIASERR(
"Input is complex or not of double precision!");
119 const unsigned int m = mxGetM(mxVec);
120 const unsigned int n = mxGetN(mxVec);
124 BIASERR(
"Input is no vector, dim: " <<m<<
"x" <<n<<
" !");
130 for (
unsigned int i=0; i<((m>n)?m:n); i++)
131 vec[i] = mxGetPr(mxVec)[i];
141 BIASERR(
"Given mxArray-pointer is NULL!");
146 if (!mxIsDouble(mxMat) || mxIsComplex(mxMat)) {
147 BIASERR(
"Input is complex or not of double precision!");
151 const unsigned int m = mxGetM(mxMat);
152 const unsigned int n = mxGetN(mxMat);
156 const double* in = mxGetPr(mxMat);
157 for (
unsigned int j=0; j<n; j++)
158 for (
unsigned int i=0; i<m; i++)
159 mat[i][j] = in[j*m + i];
169 BIASERR(
"Given mxArray-pointer is not NULL, memory leak possible!");
173 mxVec = mxCreateDoubleMatrix(vec.
Size(), 1, mxREAL);
176 BIASERR(
"Error allocating mxArray, with (m,n)=("<<vec.
Size()
181 for (
unsigned int i=0; i<vec.
Size(); i++)
182 mxGetPr(mxVec)[i] = vec[i];
192 BIASERR(
"Given mxArray-pointer is not NULL, memory leak possible!");
196 unsigned int m = mat.
GetRows();
197 unsigned int n = mat.
GetCols();
199 mxMat = mxCreateDoubleMatrix(m, n, mxREAL);
202 BIASERR(
"Error allocating mxArray, with (m,n)=("<<m<<
","<<n<<
")");
206 for (
unsigned int j=0; j<n; j++)
207 for (
unsigned int i=0; i<m; i++)
208 mxGetPr(mxMat)[j*m + i] = mat[i][j];
215 #endif //__MATLABINTERFACE_HH__
int MxArrToBIASMatrix(const mxArray *mxMat, BIAS::Matrix< double > &mat)
Convert MATLAB-array to BIAS::Matrix.
int BIASVectorToMxArr(const BIAS::Vector< double > &vec, mxArray *&mxVec)
Convert BIAS::Vector to MATLAB-array, inclusive allocation!
int MxArrToBIASVector(const mxArray *mxVec, BIAS::Vector< double > &vec)
Convert MATLAB-array to BIAS::Vector.
Matrix< T > & newsize(Subscript M, Subscript N)
unsigned int Size() const
length of the vector
int BIASMatrixToMxArr(const BIAS::Matrix< double > &mat, mxArray *&mxMat)
Convert BIAS::Matrix to MATLAB-array, inclusive allocation!
Vector< T > & newsize(Subscript N)
unsigned int GetRows() const
unsigned int GetCols() const