Prev: saved figure is blank
Next: Simulink FFT
From: genkideska Nishitani on 6 Apr 2010 11:23 Hi, I am trying to write some of my code as C mex file. I have problems when using functions like log() or pow(). I have written this very simple test file below (called test.c). It compiles but when I call it >> test([2 2]) I get >> ans = >> 0.0e+000 * >> 1.#INF 1.#INF Can anybody please tell me what is wrong?? void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ]) { double *v,*res; m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); /* Create the array */ plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL); res = mxGetPr(plhs[0]); v = mxGetPr(prhs[0]); res[0]=v[0]; res[1]=pow(2,2); }
From: Roger Stafford on 6 Apr 2010 12:11 "genkideska Nishitani" <marcel1978(a)gmx.de> wrote in message <hpfjkm$qmm$1(a)fred.mathworks.com>... > Hi, > > I am trying to write some of my code as C mex file. I have problems when using functions like log() or pow(). I have written this very simple test file below (called test.c). It compiles but when I call it > > >> test([2 2]) > > I get > > >> ans = > >> 0.0e+000 * > >> 1.#INF 1.#INF > > Can anybody please tell me what is wrong?? > > void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ]) > { > double *v,*res; > m = mxGetM(prhs[0]); > n = mxGetN(prhs[0]); > /* Create the array */ > plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL); > res = mxGetPr(plhs[0]); > v = mxGetPr(prhs[0]); > > res[0]=v[0]; > res[1]=pow(2,2); > } A couple of suggestions. You should declare int m, n; for variables m and n, and you need: #include "mex.h" at the beginning if you don't already have it. I believe you also need to use mexCallMATLAB to call on the 'pow' matlab function properly. See http://www.mathworks.com/support/tech-notes/1600/1605.html?BB=1 in the section "Using mexCallMATLAB" to learn how to do this. Roger Stafford
From: Jan Simon on 6 Apr 2010 16:11 Dear Genkideska! > >> test([2 2]) > > I get > > >> ans = > >> 0.0e+000 * > >> 1.#INF 1.#INF Strange answer. I've never seen "1.#INF" in Matlab. > void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ]) > { > double *v,*res; > m = mxGetM(prhs[0]); > n = mxGetN(prhs[0]); > /* Create the array */ > plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL); > res = mxGetPr(plhs[0]); > v = mxGetPr(prhs[0]); > > res[0]=v[0]; > res[1]=pow(2,2); > } Please display the results inside the Mex already: res[0] = v[0]; res[1] = pow(2.0, 2.0); mexPrintf("%g %g\n", rev[0], rev[1]); The best idea I have is that you call a wrong "test" function. Please check this with: which test -all Jan
From: James Tursa on 7 Apr 2010 02:44 "genkideska Nishitani" <marcel1978(a)gmx.de> wrote in message <hpfjkm$qmm$1(a)fred.mathworks.com>... > Hi, > > I am trying to write some of my code as C mex file. I have problems when using functions like log() or pow(). I have written this very simple test file below (called test.c). It compiles but when I call it > > >> test([2 2]) > > I get > > >> ans = > >> 0.0e+000 * > >> 1.#INF 1.#INF > > Can anybody please tell me what is wrong?? > > void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ]) > { > double *v,*res; > m = mxGetM(prhs[0]); > n = mxGetN(prhs[0]); > /* Create the array */ > plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL); > res = mxGetPr(plhs[0]); > v = mxGetPr(prhs[0]); > > res[0]=v[0]; > res[1]=pow(2,2); > } You are missing the prototype for the pow function. Without the prototype, the compiler will not necessarily know that it needs to promote the constant int arguments of 2,2 to double arguments 2.0,2.0, and also the compiler will not know that pow returns a double ... it will assume that it returns an int and of course interpret the returned bit pattern incorrectly. Always use prototypes for functions that you use. In this case, you need mex.h for the MATLAB API stuff and math.h for the pow function. Also, as Jan wrote, you need to declare m and n. e.g., #include <math.h> #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ]) { double *v,*res; mwSize m, n; m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); /* Create the array */ plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL); res = mxGetPr(plhs[0]); v = mxGetPr(prhs[0]); res[0]=v[0]; res[1]=pow(2,2); } James Tursa
From: genkideska Nishitani on 7 Apr 2010 05:00 dear all, You are right, I have missed to put #include <math.h> in the code! thanks a lot for your help!! genkideska
|
Pages: 1 Prev: saved figure is blank Next: Simulink FFT |