in line 11,12
you have defined x,y
as 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,y
as 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);