20 #ifndef MATLAB_MEX_FILE 21 #define MATLAB_MEX_FILE 48 return mxGetClassID(array) == mxDOUBLE_CLASS && !mxIsComplex(array);
53 return mxGetClassID(array) == mxSINGLE_CLASS && !mxIsComplex(array);
58 return mxGetClassID(array) == mxINT32_CLASS && !mxIsComplex(array);
63 return mxGetClassID(array) == mxLOGICAL_CLASS && !mxIsComplex(array);
67 template <>
inline bool mexCheckType<std::complex<double> >(
const mxArray* array) {
68 return mxGetClassID(array) == mxDOUBLE_CLASS && mxIsComplex(array);
72 template <>
inline bool mexCheckType<std::complex<float> >(
const mxArray* array) {
73 return mxGetClassID(array) == mxSINGLE_CLASS && mxIsComplex(array);
79 CheckSize(
const mxArray* array,
const int m,
const int n)
81 const mwSize* dims=mxGetDimensions(array);
82 int _m=
static_cast<int>(dims[0]);
83 int _n=
static_cast<int>(dims[1]);
84 return _n==n && _m==m;
94 if (
typeid(alpha_v) ==
typeid(alpha_v2)) {
95 alpha_v2=
reinterpret_cast<T*
>(alpha_v);
97 alpha_v2 =
new T[alpha_pB[M]];
98 for (
mwSize i = 0; i<alpha_pB[M]; ++i) alpha_v2[i] = static_cast<T>(alpha_v[i]);
100 if (
typeid(alpha_r2) ==
typeid(alpha_r)) {
101 alpha_r2=
reinterpret_cast<int*
>(alpha_r);
102 alpha_pB2=
reinterpret_cast<int*
>(alpha_pB);
103 alpha_pE2=
reinterpret_cast<int*
>(alpha_pE);
105 alpha_r2=
new int[alpha_pB[M]];
106 for (
mwSize i = 0; i<alpha_pB[M]; ++i) alpha_r2[i]=static_cast<int>(alpha_r[i]);
107 alpha_pB2=
new int[M+1];
108 for (
int i = 0; i<=M; ++i) alpha_pB2[i]=static_cast<int>(alpha_pB[i]);
109 alpha_pE2=alpha_pB2+1;
114 template <
typename T>
117 double* alpha_v,
mwSize* alpha_r)
119 if (
typeid(alpha_v) !=
typeid(alpha_v2)) {
122 if (
typeid(alpha_r2) !=
typeid(alpha_r)) {
133 template <
typename T>
141 return mxCreateNumericMatrix(static_cast<mwSize>(m), static_cast<mwSize>(n),mxDOUBLE_CLASS,mxREAL);
149 return mxCreateNumericMatrix(static_cast<mwSize>(m),static_cast<mwSize>(n),mxSINGLE_CLASS,mxREAL);
153 template <
typename T>
168 return mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL);
184 return mxCreateNumericArray(3,dims,mxSINGLE_CLASS,mxREAL);
189 template <
typename T>
205 return mxCreateNumericArray(4,dims,mxDOUBLE_CLASS,mxREAL);
221 return mxCreateNumericArray(4,dims,mxSINGLE_CLASS,mxREAL);
226 template <
typename T>
230 return CreateMatrix<T>(1,1);
234 template <
typename T>
237 int M,
int n,
int nzmax,
const T* v,
const int* r,
const int* pB)
239 matlab_mat=mxCreateSparse(K,M,nzmax,mxREAL);
240 double* Pr=mxGetPr(matlab_mat);
241 for (
int i = 0; i<nzmax; ++i) Pr[i]=static_cast<double>(v[i]);
242 mwSize* Ir=mxGetIr(matlab_mat);
243 for (
int i = 0; i<nzmax; ++i) Ir[i]=static_cast<mwSize>(r[i]);
244 mwSize* Jc=mxGetJc(matlab_mat);
246 for (
int i = 0; i<=n; ++i) Jc[i]=static_cast<mwSize>(pB[i]);
250 template <
typename T>
254 mxArray *pr_field = mxGetField(pr_struct,0,name);
256 mexPrintf(
"Missing field: ");
259 return static_cast<T
>(mxGetScalar(pr_field));
266 mxArray *pr_field = mxGetField(pr_struct,0,name);
268 mexPrintf(
"Missing field: ");
271 mxGetString(pr_field,field,length);
277 mwSize buflen = mxGetNumberOfElements(pr) + 1;
278 char* buf =
reinterpret_cast<char*
>(mxCalloc(buflen,
sizeof(
char)));
281 if (mxGetString(pr, buf, buflen) != 0)
282 mexErrMsgTxt(
"Could not convert string data.");
283 str = std::string(buf);
289 mwSize buflen = mxGetNumberOfElements(pr) + 1;
290 char* buf =
reinterpret_cast<char*
>(mxCalloc(buflen,
sizeof(
char)));
293 if (mxGetString(pr, buf, buflen) != 0)
294 mexErrMsgTxt(
"Could not convert string data.");
295 return std::string(buf);
302 mxArray *pr_field = mxGetField(pr_struct,0,name);
304 mexPrintf(
"Missing field: ");
312 template <
typename T>
318 mxArray *pr_field = mxGetField(pr_struct,0,name);
319 return pr_field ? (T)(mxGetScalar(pr_field)) : def;
328 mxArray *pr_field = mxGetField(pr_struct,0,name);
329 return pr_field ?
GetString(pr_field) : def;
337 mxArray *pr_field = mxGetField(pr_struct,0,name);
345 mexEvalString(
"pause(0.0000000001);");
348 template <
class TMatrixType>
350 SaveMatrixToMatFile (
const TMatrixType& matrix,
const int NumberRows,
const int NumberColumns,
const std::string fileName,
const std::string varibleName )
353 pmat = matOpen(fileName.c_str(),
"w");
357 mxArray *pa1= mxCreateDoubleMatrix(NumberRows,NumberColumns,mxREAL);
360 double * data = (
double*)mxGetData(pa1);
361 for (
int i = 0; i < NumberRows; i += 1 )
362 for (
int j = 0; j < NumberColumns; j += 1 )
363 data[j*NumberRows+i] = matrix(i,j);
365 status = matPutVariableAsGlobal(pmat, varibleName.c_str(), pa1);
369 status = matClose(pmat);
373 template <
class TMatrixType>
378 pmat = matOpen(fileName.c_str(),
"r");
382 mxArray *pa1= matGetVariable(pmat, varibleName.c_str());
384 const mwSize* dims = mxGetDimensions(pa1);
385 int row =
static_cast<int>(dims[0]);
386 int column =
static_cast<int>(dims[1]);
388 double * data = (
double*)mxGetData(pa1);
389 for (
int i = 0; i < row; i += 1 )
390 for (
int j = 0; j < column; j += 1 )
391 matrix(i,j) = data[j*row+i];
394 status = matClose(pmat);
mxArray * GetArrayStruct(const mxArray *pr_struct, const char *name)
void SaveMatrixToMatFile(const TMatrixType &matrix, const int NumberRows, const int NumberColumns, const std::string fileName, const std::string varibleName)
mxArray * CreateMatrix< float >(int m, int n)
Create a m x n float matrix.
mxArray * CreateImage< double >(int h, int w, int V)
Create a h x w x V double image.
mxArray * CreateImage(int h, int w, int V)
Create a h x w x V image.
void CreateCopySparse(T *&alpha_v2, int *&alpha_r2, int *&alpha_pB2, int *&alpha_pE2, double *alpha_v, mwSize *alpha_r, mwSize *alpha_pB, mwSize *alpha_pE, int M)
mxArray * Create4DImage(int h, int w, int V, int dim)
Create a h x w x V x dim image.
bool CheckSize(const mxArray *array, const int m, const int n)
Check the size of a 2D-array.
bool mexCheckType< int >(const mxArray *array)
Check the type of an array (int)
void ConvertSpMatrix(mxArray *&matlab_mat, int K, int M, int n, int nzmax, const T *v, const int *r, const int *pB)
convert sparse matrix to Matlab sparse matrix
mxArray * CreateScalar()
Create a scalar.
mxArray * CreateMatrix< double >(int m, int n)
Create a m x n double matrix.
mxArray * Create4DImage< double >(int h, int w, int V, int dim)
void DeleteCopySparse(T *&alpha_v2, int *&alpha_r2, int *&alpha_pB2, int *&alpha_pE2, double *alpha_v, mwSize *alpha_r)
Delete a sparse matrix which has been created using createCopySparse.
#define utlGlobalException(cond, expout)
bool CheckField(const mxArray *pr_struct, const char *name)
get a scalar from a struct
void super_flush(std::ostream &stream)
bool mexCheckType< bool >(const mxArray *array)
Check the type of an array (int)
void ReadMatFileToMatrix(const std::string fileName, const std::string varibleName, TMatrixType &matrix)
bool mexCheckType(const mxArray *array)
Check the type of an array.
mxArray * Create4DImage< float >(int h, int w, int V, int dim)
void GetStringStruct(const mxArray *pr_struct, const char *name, char *field, const mwSize length)
get a scalar from a struct
bool mexCheckType< double >(const mxArray *array)
Check the type of an array (double)
mxArray * CreateMatrix(int m, int n)
Create a m x n matrix.
void GetString(const mxArray *pr, std::string &str)
T GetScalarStruct(const mxArray *pr_struct, const char *name)
get a scalar from a struct
bool mexCheckType< float >(const mxArray *array)
Check the type of an array (float)
T GetScalarStructDef(const mxArray *pr_struct, const char *name, const T def)
get a scalar from a struct and provide a default value
mxArray * CreateImage< float >(int h, int w, int V)
Create a h x w x V float image.