How can I get MEX function to return the right value?
Afficher commentaires plus anciens
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 commentaires
Adam
le 12 Sep 2016
Where in the algorithm is it suggested that 5 5 5 is in any way what is expected?
Cheryl Wong
le 12 Sep 2016
Adam
le 12 Sep 2016
And what result does it give?
Cheryl Wong
le 12 Sep 2016
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!