Passing int to mex file
12 vues (au cours des 30 derniers jours)
I wrote a MEX routine that calls a C function which uses "int" rather than double.
The way I fetch the array from MatLab is through a command like:
nc = (int *) mxGetPr(prhs);
However, this works only if, when calling the function from MatLab, I do:
Otherwise, it crashes, as it calculates an index incorrectly, which I then use to access some elements of an array.
I tried passing the array as a double, and then casting it to a new array within the mex file. Like this:
nc_double = (double *) mxGetPr(prhs);
for (int i=0; i<size_nc; n++)
nc[i] = (int) nc_double[i];
It compiled, but have me an error: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) using the debugger.
I wonder if there is a more robust way of doing this? Someone on the Matlab side might forget to convert the culprits to int32 and crash the program.
Friedrich le 5 Août 2014
Modifié(e) : Friedrich le 5 Août 2014
have you tried
int a = (int)*mxGetPr(prhs);
This will work for double values only. In order to make this work for all kinf of data you would need to get the classID of the input argument using mxGetClassID and cast accordingly.
Or if you are lazy call back to MATLAB and let it do the cast:
a = *(int*)mxGetData(lhs);