MATLAB: How to use mxCreateNumericArray

integersmexmxcreatenumericarray

I am building a mex function, but am having difficulty creating a vector of integers. My code throws about 10 errors, all of which around the declaration of 'gn' and plhs[0]. I'm just trying to make a xynum long vector of class int16. Why is it so difficult?
void findnegs(double *phi, int gnum, int xynum, int *gn, double *adj, double *newphi, double *dphidtSum)
{
long i,g;
double u,s;
for (i = 0; i<xynum; i++)
{
s = 0;
for (g = 0; g<gnum; g++)
{
u = dphidtSum[g+i*gnum]/phi[g+i*gnum];
if (newphi[g+i*gnum]<0 & u<s)
{
s = u;
gn[i] = g;
adj[i] = -1/u;
}
}
}
}
/* The gateway function that replaces the "main".
*plhs[] - the array of output values
*prhs[] - the array of input values
*/
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* DECLARATIONS*/
double *phi, *newphi, *dphidtSum, *adj;
int gnum, xynum, *gn;
/* INPUTS */
gnum = mxGetScalar(prhs[0]);
xynum = mxGetScalar(prhs[1]);
phi = mxGetPr(prhs[2]);
newphi = mxGetPr(prhs[3]);
dphidtSum = mxGetPr(prhs[4]);
/* OUTPUTS */
plhs[0] = mxCreateNumericArray(xynum,1,mxINT16_CLASS); /*Creates a matrix*/
plhs[1] = mxCreateDoubleMatrix(xynum,1,mxREAL); /*Creates a matrix*/
gn = mxGetInt16s(plhs[0]); /*Set to be the first of the output values*/
adj = mxGetPr(plhs[1]); /*Set to be the first of the output values*/
/* CALL ROUTINE */
findnegs(phi,gnum,xynum,gn,adj,newphi,dphidtSum);
}

Best Answer

This line is incorrect (wrong function and signature):
plhs[0] = mxCreateNumericArray(xynum,1,mxINT16_CLASS); /*Creates a matrix*/
It should be this instead:
plhs[0] = mxCreateNumericMatrix(xynum,1,mxINT16_CLASS,mxREAL); /*Creates a matrix*/
Also, these lines will not work:
int gnum, xynum, *gn;
:
gn = mxGetInt16s(plhs[0]); /*Set to be the first of the output values*/
An int on your system is almost surely a 32-bit integer, not a 16-bit integer. You need to make sure your pointer definition matches exactly what is in memory. E.g., you can probably use a short int for this:
int gnum, xynum;
short *gn;
:
gn = mxGetInt16s(plhs[0]); /*Set to be the first of the output values*/
That will mean you need to change this signature:
void findnegs(double *phi, int gnum, int xynum, int *gn, double *adj, double *newphi, double *dphidtSum)
to this instead also:
void findnegs(double *phi, int gnum, int xynum, short *gn, double *adj, double *newphi, double *dphidtSum)
But this begs the question, are integer calculations in the findnegs function now going to overflow your 16-bit integers in gn? You may need to adjust your code to make sure this isn't a problem.