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!