How can I get MEX function to return the right value?
显示 更早的评论
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 个评论
Adam
2016-9-12
Where in the algorithm is it suggested that 5 5 5 is in any way what is expected?
Cheryl Wong
2016-9-12
Adam
2016-9-12
And what result does it give?
Cheryl Wong
2016-9-12
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Write 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!