Problem with double to integer casting in mex file.

조회 수: 10 (최근 30일)
Geert
Geert 2012년 12월 13일
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
James Tursa
James Tursa 2012년 12월 13일
@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
José-Luis 2012년 12월 13일
True, my bad. I was thinking mxGetM.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2012년 12월 13일
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);

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by