Converting mxArray to MatInt


John Knowles
11-12-2013, 02:51 AM
Here is a C++ code that I was hoping to use to convert double mxArray to integer arrays. I've never used MatInt before, so would like advice, as I'm getting an error on the return line (see below). Is there a cleaner way to declare the integer object? I guess it cant be in the conditional statements, but I fear that as Im redifining I'm leaking memory. Ive included #include "nr3matlab.h" as this is being run in a mex file.


MatInt *mxArray2MatInt(mxArray * mx )
{
double *pr;
int *ip,ndims;
const mwSize *dim_array ;
mwSize i,j, n;
MatInt IntNRMat;
pr = mxGetPr(mx);
n = mxGetNumberOfElements(mx);
ndims = mxGetNumberOfDimensions(mx);
dim_array=mxGetDimensions(mx);
if(ndims==1)
{
MatInt IntNRMat(1,dim_array[0],ip);
for( i=0; i<n; i++ )IntNRMat[0][i] = pr[i];
}
if(ndims>1)
{
MatInt IntNRMat(dim_array[0],dim_array[1],ip);
for( i=0; i<n; i++ )IntNRMat[i][j] = pr[i*dim_array[1]+j];
}
return IntNRMat; // "No viable conversion from 'MatInt' (aka 'NRmatrix<Int>') to 'MatInt *' (aka 'NRmatrix<Int> *')"
}