Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
mex problem

Subject: mex problem

From: genkideska Nishitani

Date: 6 Apr, 2010 15:23:02

Message: 1 of 5

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);
}

Subject: mex problem

From: Roger Stafford

Date: 6 Apr, 2010 16:11:20

Message: 2 of 5

"genkideska Nishitani" <marcel1978@gmx.de> wrote in message <hpfjkm$qmm$1@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

Subject: mex problem

From: Jan Simon

Date: 6 Apr, 2010 20:11:20

Message: 3 of 5

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

Subject: mex problem

From: James Tursa

Date: 7 Apr, 2010 06:44:05

Message: 4 of 5

"genkideska Nishitani" <marcel1978@gmx.de> wrote in message <hpfjkm$qmm$1@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

Subject: mex problem

From: genkideska Nishitani

Date: 7 Apr, 2010 09:00:27

Message: 5 of 5

dear all,

You are right, I have missed to put

#include <math.h>

in the code! thanks a lot for your help!!

genkideska

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us