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 Center および File ExchangeWrite 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