mexGetData() outputs zeros
Show older comments
I have the following matlab mex function:
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] ){
//Declare variables for the input arguments.
size_t lengthh; /* input scalar */
double *inNoise; /* 1xN input matrix */
float (*inGlucose)[12]; // 1xN input matrix float (*PatchSet)[64];
double *inDates; /* 1xN input matrix */
if(nrhs != 4) {
mexErrMsgIdAndTxt("MyToolbox:fullLoop:nrhs", "4 inputs required.");
}
/*if(nlhs != 1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs",
"One output required.");
}*/
(void) nlhs; (void) plhs;
// make sure the first input argument is an array
if( mxIsComplex(prhs[0]) || !mxIsDouble(prhs[0]) ) {
mexErrMsgIdAndTxt("MyToolbox:fullLoop:notDouble","1:Input matrix must be type double.");
}
if( !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) ) {
mexErrMsgIdAndTxt("MyToolbox:fullLoop:notDouble","2:Input matrix must be type double.");
}
if( !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2])|| mxGetNumberOfElements(prhs[2]) < 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","3:Input matrix must be type long int.");
}
if( mxIsComplex(prhs[3]) || mxGetNumberOfElements(prhs[3])!=1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","4:Input scalar must be type double.");
}
// check that number of rows in second input argument is 1
if(mxGetM(prhs[0])!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","5:1st argument must be a row vector.");
}
if(mxGetM(prhs[1])!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","6:2nd argument must be a row vector.");
}
if(mxGetM(prhs[2])!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","7:3rd argument must be a row vector.");
}
printf("Sizes: %d %d %d \n", mxGetNumberOfElements(prhs[0]), mxGetNumberOfElements(prhs[1]), mxGetNumberOfElements(prhs[2]));
/******************************************PROBLEM*****************************************************/
inNoise = (double *)mxGetData(prhs[0]);//mxGetData
inGlucose = (float (*)[12]) mxGetData(prhs[1]);//PatchSet = (float (*)[64]) mxGetData(prhs[0]);
lengthh = mxGetScalar(prhs[3]);
/******************************************************************************************************/
printf("length: %d\n",lengthh);
int i;
for (i=0;i<lengthh;i++){
printf("%f -- %f\n",inGlucose[i],inNoise[i]);
}
return;
}
The problem is in the section separated by the line of asterisks.
I followed this tutorial to cast the arrays. Although the double array (inNoise) is properly read by the program, it returns an array of 0's for inGlucose.
There seems to be a fix to this in Matlab 2018A, but for unavoidable reasons I could only find 2016B.
Can anyone help me here?
Answers (1)
James Tursa
on 17 Aug 2018
Edited: James Tursa
on 17 Aug 2018
1) Your check on prhs[1] requires that it is a double, but your pointer is to a float. So that mismatch will never work.
float (*inGlucose)[12]; // 1xN input matrix float (*PatchSet)[64];
:
if( !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) ) {
:
inGlucose = (float (*)[12]) mxGetData(prhs[1]);//PatchSet = (float (*)[64]) mxGetData(prhs[0]);
You need to make these consistent. Either make it all double or make it all float.
2) You have set up two levels of indirection for accessing prhs[1]. Not sure why you think you need this complication (why not just use a simple pointer to float) unless there is other code you haven't shown that requires it. Let's assume that you need it. Then you need two levels of dereferencing to get at the data. E.g.,
printf("%f -- %f\n",inGlucose[0][i],inNoise[i]); // <-- Note the added [0]
You have set up inGlucose as type "pointer to array of 12 floats". So inGlucose[0] is the first "array of 12 floats" that it points to, and inGlucose[0][i] is the i'th element of that array.
3) I have no idea why you think MATLAB version (e.g., R2018a) is relevant to this problem.
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!