Question

I have some trouble generating a MEX file from the following C function:

#include <math.h>
#include <mex.h>    

void mexFunction(int nlhs, mxArray *plhs[], /* Output variables */
                 int nrhs, const mxArray *prhs[]) /* Input variables */
{
    #define y plhs[0]
    #define x prhs[0]

    double y, x, p;
    int Y, X;

    mexPrintf ("x = %d",x);
    if(nrhs < 1 or nrhs > 2) /* Check the number of arguments */
        mexErrMsgTxt("Wrong number of input arguments.");
    else if(nlhs > 1)
        mexErrMsgTxt("Too many output arguments.");

    if(nrhs == 1) /* If p is unspecified, set it to a default value */
        c = 3.0;

    y=c*x*x;

    X=mxIsDouble(x);
    Y=mxIsDouble(y);

    mexPrintf ("the value for y is %d",Y);
}

I have done a lot of research but I still cannot seem to get the concept. My task is simply to generate a MEX file that can generate y=3x^2 when inputting x in matlab. I saved the file as quadratic.c. When I type

`mex quadratic.c`

in matlab command, I get:

quadratic.c 
quadratic.c(15) : error C2143: syntax error : missing ';' before 'type' 
quadratic.c(15) : error C2143: syntax error : missing ';' before ',' 
quadratic.c(15) : error C2143: syntax error : missing ';' before ',' 
quadratic.c(16) : error C2143: syntax error : missing ';' before 'type' 
quadratic.c(18) : error C2143: syntax error : missing ')' before ';' 
quadratic.c(18) : error C2059: syntax error : ')' 
quadratic.c(19) : error C2146: syntax error : missing ')' before identifier 'or' 
quadratic.c(19) : error C2065: 'or' : undeclared identifier 
quadratic.c(19) : error C2146: syntax error : missing ';' before identifier 'nrhs' 
quadratic.c(19) : error C2059: syntax error : ')' 
quadratic.c(19) : error C2143: syntax error : missing ';' before '{' 
quadratic.c(19) : warning C4552: '>' : operator has no effect; expected operator with side-effect 
quadratic.c(21) : error C2181: illegal else without matching if 
quadratic.c(25) : error C2065: 'c' : undeclared identifier 
quadratic.c(25) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 
quadratic.c(27) : error C2143: syntax error : missing ';' before '=' 
quadratic.c(29) : error C2065: 'X' : undeclared identifier 
quadratic.c(29) : error C2143: syntax error : missing ')' before ';' 
quadratic.c(29) : error C2059: syntax error : ')' 
quadratic.c(30) : error C2065: 'Y' : undeclared identifier 
quadratic.c(30) : error C2143: syntax error : missing ')' before ';' 
quadratic.c(30) : error C2059: syntax error : ')' 
quadratic.c(33) : error C2065: 'Y' : undeclared identifier 

  C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: Compile of 'quadratic.c' failed. 

I don't know what to do. I'm sure I did something wrong but I cant figure it out.

Was it helpful?

Solution

in line 11,12 you have defined x,yas the output and input of the function

11:   #define y plhs[0]
12:   #define x prhs[0]

but in line 15,16 you have defined x,yas variables of type double

15:   double y, x, p;
16:   int Y, X;

you should change the name of the variables in line 11,12 or that in line 15,16 to make them different, because as you have written 11: #define y plhs[0], every y in the program is actually replaced with plhs[0] by the compiler, and this is the pointer to the output data in Matlab.

thus, if you have replaced, say double x by double xVal, before accessing the value at line 18, you should first get its value from prhs[0]

18:   mexPrintf ("x = %d",x);

so you should write this before line 18:

xVal = mxGetPr(x)[0]; %assuming the input is real valued, and it is only a number.

in addition, before accessing the value of y, you should first get its value;

double yVal;
yVal = mxGetPr(y)[0];

what's important is that prhs[0] and plhs[0] are pointers to the actual data(value, size, type, etc.) and they are not the value that can be used directly.

besides, at line 19:

19:   if(nrhs < 1 or nrhs > 2) 

I'm not so sure if the or operator in C is or, instead, you may write

19:   if(nrhs < 1 || nrhs > 2) #replaced "or" with two lines

at line 25:

25: c = 3.0;

but C is not like Matlab, you should define the variable c before using it.

I'm a bit confused by the function of X and Y, because they are clearly indicators of the variable type of x,y and not the value to be printed. if you have written double yVal = mxGetPr(y)[0]; as above, the line 33 can be changed to this:

30:   Y=mxIsDouble(y);
33:   mexPrintf ("the value for y is %d",yVal);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top