错误信息Mex文件Matlab

3

有没有人遇到过这个错误信息:

未定义的函数或方法'函数名',输入参数类型为'double'。

当我编译一个mex文件时,总是出现这个错误信息。我已经仔细检查了路径,看起来应该是正确的。

这是我的代码,mex文件是amortiss.c

#include "mex.h"

/* The computational functions */
void arrayquotient(double input1, double input2, double output1)
{

   output1=input1/input2;

}


/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
/* variable declarations here */
    double input1;      /* input scalar 1 */
    double input2;       /* input scalar 2*/
    double output1;      /* output scalar 1 */   

/* code here */
    /* get the value of the scalar input1  */
   input1 = mxGetScalar(prhs[0]);

/* get the value of the scalar input2 */
   input2 = mxGetScalar(prhs[1]);

/* get the value of the scalar input3 */
   input3 = mxGetScalar(prhs[2]);

/* create the output scalar1 */
    plhs[0] = mxCreateDoubleScalar(input1/input2);

/* get the value of the scalar output1 */
   output1 = mxGetScalar(plhs[0]);

/* call the computational routines */
arrayquotient(input1,input2,output1);
}

我添加了路径(命令add path)以确保mex文件amortiss.c存在。然后我创建了一个名为arrayquotient.m的.m文件,在其中只写了我的函数声明:
function c = arrayquotient(a,b)
但是,在编译时,出现了另一个错误消息:
Error in ==> arrayquotient at 1
function c=arrayquotient(a,b)

??? Output argument "c" (and maybe others) not assigned during call to
"C:\Users\hp\Documents\MATLAB\codes_Rihab\arrayquotient.m>arrayquotient".

你确定你实际上正在编译MEX吗?如果你没有真正编译函数,通常会出现“未定义的函数等”错误。 - wakjah
我有点困惑。你要编写的函数是叫做amortiss还是arrayquotient?除非你使用正确的标志进行编译,否则你编译出来的mex文件将会是amortiss。请报告which arrayquotient -allwhich amortiss -all的结果。 - nhowe
arrayquotient中对参数output1的赋值不会改变mexFunction中的另一个变量output1。如果在mexFunction中对output1进行赋值,仅此是不会改变plhs[0]中的值的。您应该检查nrhs以避免在错误输入时使Matlab崩溃。 - aschepler
感谢大家的回应。我的Mex文件名为“amortiss.c”,其中函数名为“arrayquotient”。我使用Microsoft Visual C++ 2008 Express编译器编译了Mex文件,但是一旦进入文件夹,我找不到“ .mexw32”文件,也找不到“arrayquotient”函数或“amortiss.c”文件。您认为这是问题所在吗?现在我该怎么办?非常感激您的帮助!! - Mayar Rihab
1个回答

1

amortiss.c函数是一个c文件,不能直接在Matlab中执行。
你创建的arrayquotient.m函数是一个空函数,不给它的输出参数c赋值。

你需要做的是对c文件amortiss.c进行mex编译,生成一个mex文件amortiss.mexw32(根据你的架构,扩展名会有所不同。使用mexext来找到你需要寻找的扩展名)。

在Matlab中,设置你的mex编译器:

>> mex -setup

您将被指示从已安装在您的计算机上并被Matlab识别的编译器中进行选择。

一旦设置好mex编译器,您可以继续对c文件进行编译。

>> mex -O -largeArrayDims amortiss.c

然后你将会得到一个名为amortiss.mexXXX('XXX'取决于你的架构)的mex文件。
你可以像调用其他函数一样调用这个函数。

>> c = amortiss( a, b );

@ Shai,谢谢你的回复。我按照你说的做了,选择了编译器:Microsoft Visual C++ 2008 Express。我还删除了"M"文件“arrayquotient.m”,然后这是我键入的内容:mex amortiss.c
c =2.; b = 3.; d = arrayquotient(c,b) ??? Invalid MEX-file 'C:\Users\hp\Documents\MATLAB\arrayquotient.mexw32':C:\Users\hp\Documents\MATLAB\arrayquotient.mexw32不是有效的Win32应用程序。现在我该怎么办??我非常感谢你的帮助!!
- Mayar Rihab
>> mexext 的输出是什么? - Shai
你应该删除 arrayquotient.mex32 并运行 >> c = amortiss( a, b ); - Shai
1
我改了函数的名称,现在它运行得很好。就是这么简单。无论如何,非常感谢您的帮助。目前,它正常运行。非常感谢您的建议和帮助。 - Mayar Rihab
@Rarepearl 谢谢!随时发表你的CLIPS问题,也许还有其他人可以帮助你。 - Shai
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接