How can I get MEX function to return the right value?
Show older comments
Below is an example of the MEX code I am trying to implement, I am wondering why does the code return different values instead of 5 5 5?
CODE:
#include "mex.h"
void merge(int x, float *y, int n){
for (int i = 0; i < n; i++){
y[i] = x;
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double x;
float *y;
int n;
if(nrhs !=2 )
mexErrMsgTxt("Wrong number of input arguments.");
x = mxGetScalar(prhs[0]);
n = mxGetScalar(prhs[1]);
/* --- output --- */
plhs[0] = mxCreateDoubleMatrix(1, n, mxREAL);
y = (float *)mxGetPr(plhs[0]);
// mexPrintf("Before %30.25f\n", y);
merge(x,y,n);
mexPrintf("The first value is %30.25f\n", y[0]);
mexPrintf("The second value is %30.25f\n", y[1]);
mexPrintf("The third value is %30.25f\n", y[2]);
// return;
}
4 Comments
Adam
on 12 Sep 2016
Where in the algorithm is it suggested that 5 5 5 is in any way what is expected?
Cheryl Wong
on 12 Sep 2016
Adam
on 12 Sep 2016
And what result does it give?
Cheryl Wong
on 12 Sep 2016
Accepted Answer
More Answers (0)
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!