问题
I want to pass an integer array from MATLAB to C. I've tried using the type uint64_T but it keeps crashing. Getting a double pointer using mxGetPr() works fine, but I need to typecast the content into an integer and doing so in every iteration might be slow.
I've tried this in the mexFunction:
uint64_T *l;
l= (uint64_T *)mxGetData(prhs[1]);
The function calling this variable is of this form:
void XAction( const double *v, const uint64_T *l, double *w)
{
for (j=c; j; j--)
for (i=r-1; i; i--)
w[i] =v[l[r*j +i]]
}
Am I making a mistake in the typecasting?
I've seen this answer : how to read an integer array in MEX-function but I haven't been able to make progress.
回答1:
For l= (uint64_T *)mxGetData(prhs[1]);
to work correctly, the data passed from MATLAB to your mex already has to be of type uint64.
Make sure you are passing uint64 array from MATLAB to your mex file. If you call myMex([1 2 3])
the array passed is not integer. It is double.
You can't convert a double array to int array by casting the pointer. Either you have to pass integer values from MATLAB or you have to create a new int array and fill it in a cycle by casting each value individually. To convert values to uint64 in MATLAB, you can do myMex(uint64([1 2 3]))
来源:https://stackoverflow.com/questions/16783727/using-integer-arrays-on-mex