我在从以下 C 函数生成 MEX 文件时遇到一些问题:
#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);
}
我做了很多研究,但我似乎仍然无法理解这个概念。我的任务只是生成一个MEX文件,在matlab中输入x时可以生成y=3x^2。 我将文件保存为quadratic.c。当我输入时
`mex quadratic.c`
在 matlab 命令中,我得到:
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.
我不知道该怎么办。我确信我做错了什么,但我无法弄清楚。
最佳答案
在第 11,12
行中,您已将 x,y
定义为函数的输出和输入
11: #define y plhs[0]
12: #define x prhs[0]
但在第 15,16
行中,您已将 x,y
定义为 double
类型的变量
15: double y, x, p;
16: int Y, X;
您应该更改 11,12
行或 15,16
行中的变量名称以使它们不同,因为正如您所写的 11:#define y plhs[0]
,程序中的每一个y
实际上都会被编译器替换成plhs[0]
,这就是指针到Matlab中的输出数据。
因此,如果您已将 double x
替换为 double xVal
,那么在访问第 18 行的值之前,您应该首先从 prhs 获取其值[0]
18: mexPrintf ("x = %d",x);
所以你应该在第 18 行之前写下这个:
xVal = mxGetPr(x)[0]; %assuming the input is real valued, and it is only a number.
另外,在访问y
的值之前,应该先获取它的值;
double yVal;
yVal = mxGetPr(y)[0];
重要的是 prhs[0]
和 plhs[0]
是指向实际数据(值、大小、类型等)的指针,它们不是可以直接使用的值。
此外,第 19 行:
19: if(nrhs < 1 or nrhs > 2)
我不太确定 C 中的 or
运算符是否是 or
,相反,你可以写
19: if(nrhs < 1 || nrhs > 2) #replaced "or" with two lines
第 25 行:
25: c = 3.0;
但是C不像Matlab,你应该在使用它之前定义变量c
。
我对X
和Y
的功能有点困惑,因为它们清楚地指示了x,y
的变量类型> 而不是要打印的值。如果您已如上编写了 double yVal = mxGetPr(y)[0];
,则 33
行可以更改为:
30: Y=mxIsDouble(y);
33: mexPrintf ("the value for y is %d",yVal);
关于从 C 编译 MEX 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23452535/