Problem with double to integer casting in mex file.
Show older comments
I have written a mex function (see below) which modifies two arrays (adata and bata), coming from another matlab function. The function does not work however, because the double n1_double is not casted into the integer n1.
Can anyone explain why this casting does not work?
In the mexfile fNbC is used as a pointer to a double array. When the mexfunction is called from matlab, I don't pass the matlab double array fnbc, but the converted integer array int32(fnbc).
Would it also be possible to let the mexfile accept the integer array int32(fnbc) directly as input? This would be like:
fNbC = (int*) mxGetData(FNBC);
instead of what i do now: fNbC = (double*) mxGetPr(FNBC);
%========================================================
#include "mex.h"
/* Macros for the ouput and input arguments */ #define FLXCOEFF1 prhs[0] #define FLXCOEFF2 prhs[1] #define FLXVAL prhs[2] #define FNBC prhs[3] #define NIF prhs[4] #define NF prhs[5] #define ADATA prhs[6] #define BDATA prhs[7] #define N prhs[8] #define ELSIZE prhs[9]
/* The computational routine */ void addLHS_fflux_to_pcellroutine( double *flxCoeff1, double *flxCoeff2, double *flxVal, double *fNbC, int nIf, int nF, double *adata, double *bdata, int n, int elsize ) { int cursor = n + 2*nIf-1; double n1_double;
for (int jF=nIf; jF<nF; jF++) {
cursor++;
n1_double = fNbC[0 + jF];
mexPrintf("n1 is %d .\n", n1_double);
int n1 = static_cast<int>(n1);
mexPrintf("n1 is %d .\n", n1);
n1--;
for (int el=0; el<elsize; el++) {
adata[el + n1*elsize] += flxCoeff1[el + jF*elsize];
adata[el + cursor*elsize] += flxCoeff2[el + jF*elsize];
bdata[el + n1*elsize] += flxVal[el + jF*elsize];
}
}
}
/* The gateway function */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *flxCoeff1, *flxCoeff2, *flxVal; double *fNbC; int nIf, nF; double *adata, *bdata; int n, elsize;
/* create a pointer to the real data in the input arrays */
flxCoeff1 = (double*) mxGetPr(FLXCOEFF1);
flxCoeff2 = (double*) mxGetPr(FLXCOEFF2);
flxVal = (double*) mxGetPr(FLXVAL);
fNbC = (double*) mxGetPr(FNBC);
adata = (double*) mxGetPr(ADATA);
bdata = (double*) mxGetPr(BDATA);
/* get the value of the scalar inputs */
nIf = (int) mxGetScalar(NIF);
nF = (int) mxGetScalar(NF);
n = (int) mxGetScalar(N);
elsize = (int) mxGetScalar(ELSIZE);
/* call the computational routine */
addLHS_fflux_to_pcellroutine(flxCoeff1,flxCoeff2,flxVal,
fNbC,nIf,nF,adata,bdata,n,elsize);
return;
}
5 Comments
First a comment: mxGetPr() returns a variable of type mwSize, not int. That can produce some errors if not handled properly. I believe mwSize is a disguised size_t, which is not necessarily 32 bits.
EDIT begin
I misread the question, mxGetPr returns a double*. You are casting double* to double*. I fail to see the point in that. The rest of the comment is a bit pointless, but as James says, it is a good idea to double check your pointers.
EDIT end
It is not clear to me where your error does happen. What message are you getting? Blindly casting is dangerous, what if your double is larger than INTMAX or smaller than INTMIN? Unexpected behavior might ensue, or your program might crash down.
James Tursa
on 13 Dec 2012
@José-Luis: mxGetPr returns a variable of type (double *), not mwSize.
James Tursa
on 13 Dec 2012
@Geert: It is a good idea to use checking functions (e.g., mxIsDouble, mxIsEmpty, mxIsComplex, mxIsSparse, etc) liberally at the beginning of your mex routine to make sure the inputs are exactly what you are expecting before getting and dereferencing the data pointers.
José-Luis
on 13 Dec 2012
True, my bad. I was thinking mxGetM.
Answers (1)
Jan
on 13 Dec 2012
It is possible to forward a INT32 array to the MEX. Then use "int32_T" as type, which is defined in "tmwtypes.h", which is included by "mex.h" already. Then:
fNbC = (int32_T *) mxGetData(FNBC);
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!