MATLAB 错误:“double”类型的输入参数的未定义函数或方法 X
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MATLAB error: Undefined function or method X for input arguments of type 'double'
提问by Todd
I'm a new user of Matlab, can you please help:
I have the following code in an .M file:
我是 Matlab 的新用户,请帮忙:
我在 .M 文件中有以下代码:
function f = divrat(w, C)
S=sqrt(diag(diag(C)));
s=diag(S);
f=sqrt(w'*C*w)/(w'*s);
I have stored this file (divrat.M) in the normal Matlab path, and therefore I'm assuming that Matlab will read the function when it's starting and that this function therefore should be available to use.
我已将此文件 (divrat.M) 存储在正常的 Matlab 路径中,因此我假设 Matlab 将在启动时读取该函数,因此该函数应该可以使用。
However, when I type
但是,当我输入
>> divrat(w, C)
I get the following error
我收到以下错误
??? Undefined function or method 'divrat' for input arguments of type 'double'.
???'double' 类型的输入参数的未定义函数或方法 'divrat'。
What is the error message telling me to do, I can't see any error in the code or the function call?
错误消息告诉我该怎么做,我在代码或函数调用中看不到任何错误?
回答by Todd
You get this error when the function isn't on the MATLAB path or in pwd.
当函数不在 MATLAB 路径或 pwd 中时,您会收到此错误。
First, make sure that you are able to find the function using:
首先,确保您能够使用以下方法找到该函数:
>> which divrat
c:\work\divrat\divrat.m
If it returns:
如果它返回:
>> which divrat
'divrat' not found.
It is not on the MATLAB path or in PWD.
它不在 MATLAB 路径或 PWD 中。
Second, make sure that the directory that contains divratis on the MATLAB path using the PATHcommand. It may be that a directory that you thought was on the path isn't actually on the path.
其次,divrat使用该PATH命令确保包含的目录位于 MATLAB 路径上。可能是您认为在路径上的目录实际上不在路径上。
Finally, make sure you aren't using a "private" directory. If divratis in a directory named private, it will be accessible by functions in the parent directory, but not from the MATLAB command line:
最后,确保您没有使用“私有”目录。如果divrat位于名为 private 的目录中,则父目录中的函数可以访问它,但不能从 MATLAB 命令行访问:
>> foo
ans =
1
>> divrat(1,1)
??? Undefined function or method 'divrat' for input arguments of type 'double'.
>> which -all divrat
c:\work\divrat\private\divrat.m % Private to divrat
回答by sundar - Reinstate Monica
As others have pointed out, this is very probably a problem with the path of the function file not being in Matlab's 'path'.
正如其他人指出的那样,这很可能是函数文件的路径不在 Matlab 的“路径”中的问题。
One easy way to verify this is to open your function in the Editor and press the F5key. This would make the Editor try to run the file, and in case the file is not in path, it will prompt you with a message box. Choose Add to Pathin that, and you must be fine to go.
验证这一点的一种简单方法是在编辑器中打开您的函数并按下F5键。这将使编辑器尝试运行该文件,如果该文件不在路径中,它将用一个消息框提示您。选择Add to Path那个,你一定没问题。
One side note: at the end of the above process, Matlab command window will give an error saying arguments missing: obviously, we didn't provide any arguments when we tried to run from the editor. But from now on you can use the function from the command line giving the correct arguments.
边注:在上述过程结束时,Matlab 命令窗口将给出一个错误,说参数丢失:显然,当我们尝试从编辑器运行时,我们没有提供任何参数。但是从现在开始,您可以从命令行使用该函数给出正确的参数。
回答by Marc
The most common cause of this problem is that Matlab cannot find the file on it's search path. Basically, Matlab looks for files in:
这个问题最常见的原因是 Matlab 在它的搜索路径上找不到文件。基本上,Matlab 在以下位置查找文件:
- The current directory (
pwd); - Directly in a directory on the path (to see the path, type
pathat the command line) - In a directory named
@(whatever the class of the first argument is)that is in any directory above.As someone else suggested, you can use the command
which, but that is often unhelpful in this case - it tells you Matlab can't find the file, which you knew already.
So the first thing to do is make sure the file is locatable on the path.Next thing to do is make sure that the file that matlab is finding (use which) requires the same type as the first argument you are actually passing. I.el, If
wis supposed to be different class, and there is adivratfunction there, butwis actually empty,[], so matlab is looking forDouble/divrat, when there is only a@(yourclass)/divrat.This is just speculation on my part, but this often bites me.
- 当前目录 (
pwd); - 直接在路径上的目录中(要查看路径,
path在命令行输入) - 在
@(whatever the class of the first argument is)上面任何目录中名为的目录中。正如其他人建议的那样,您可以使用命令
which,但在这种情况下这通常没有帮助 - 它告诉您 Matlab 找不到您已经知道的文件。
所以首先要做的是确保文件在路径上是可定位的。接下来要做的是确保 matlab 正在查找(使用 which)的文件需要与您实际传递的第一个参数相同的类型。I.el, If
w应该是不同的类,那里有一个divrat函数,但w实际上是空的[],所以 matlab 正在寻找Double/divrat,当只有一个时,@(yourclass)/divrat.这只是我的猜测,但这经常让我感到困惑。
回答by hakan
The error code indicates the function definition cannot be found. Make sure you're calling the function from the same workspace as the divrat.mfile is stored. And make sure divratfunction is not a subfunction, it should be first function declaration in the file. You can also try to call the function from the same divrat.mfile in order to see if the problem is with workspace selection or the function.
错误代码表示找不到函数定义。确保您从divrat.m存储文件的同一工作区调用该函数。并确保divrat函数不是子函数,它应该是文件中的第一个函数声明。您还可以尝试从同一divrat.m文件中调用该函数,以查看问题是与工作区选择还是该函数有关。
By the way, why didn't you simply say
对了,你为什么不直接说
s = sqrt(diag(C));
Wouldn't it be the same?
不会一样吧?
回答by Mr Fooz
Also, name it divrat.m, not divrat.M. This shouldn't matter on most OSes, but who knows...
另外,命名为divrat.m,而不是divrat.M。这在大多数操作系统上应该无关紧要,但谁知道...
You can also test whether matlab can find a function by using the whichcommand, i.e.
也可以通过which命令测试matlab能否找到一个函数,即
which divrat
回答by bastibe
The function itself is valid matlab-code. The problem must be something else.
Try calling the function from within the directory it is located or add that directory to your searchpath using addpath('pathname').
该函数本身是有效的 matlab 代码。问题一定是别的。
尝试从它所在的目录中调用该函数,或者使用 将该目录添加到您的搜索路径中addpath('pathname')。
回答by eyasu
I am pretty sure that the reason why this problem happened is because of the license of the toolbox (package) in which this function belongs in. Write which divratand see what will be the result. If it returns path of the function and the comment Has no license available, then the problem is related to the license. That means, license of the package is not set correctly. Mostly it happens if the package (toolbox) of this function is added later, i.e., after installation of the original matlab. Please check and solve the license issue, then it will work fine.
我很确定发生这个问题的原因是因为这个函数所属的工具箱(包)的许可。写下来which divrat看看会是什么结果。如果它返回函数的路径和注释Has no license available,则问题与许可证有关。这意味着,包的许可证设置不正确。大多数情况下,如果稍后添加此功能的包(工具箱),即在安装原始matlab. 请检查并解决许可证问题,然后它会正常工作。

