“类型为"double"的输入参数的未定义函数"function_name"." [英] "Undefined function 'function_name' for input arguments of type 'double'."

查看:108
本文介绍了“类型为"double"的输入参数的未定义函数"function_name"."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个经常以不同形状和大小弹出的问题是:为什么会出现以下错误消息:

A question that pops up quite frequently in different shapes and sizes is: Why do I get the following error message:

类型为'double'的输入参数的未定义函数'function_name'."

"Undefined function 'function_name' for input arguments of type 'double'."

本文试图解决可能出现此错误消息的所有不同情况,并提出解决方案.

This post attempts to address all the different scenarios where this error message can occur, and propose solutions for how it can be resolved.

推荐答案

如果您偶然发现此错误消息而又不知道这是什么意思.对此请放心:我们90%的人都用谷歌搜索了相同的短语.

If you stumble upon this error message and don't know what it means. Take comfort in this: 90% of us have googled the same phrase.

类型为'double'的输入参数的未定义函数'int'."

"Undefined function 'int' for input arguments of type 'double'."

该错误消息非常不言自明,但仍可能引起混乱. (我随机选择了'int',也可能是'train', 'table', 'my_function'或其他任何符号.)

The error message is pretty self-explanatory, but may still cause confusion. (I chose 'int' at random, it could just as well be 'train', 'table', 'my_function' or anything else).

在两种主要情况下会发生此错误:

There are two main cases where this error occur:

  1. 您正在尝试使用尚不存在的功能
  2. 您正在尝试访问尚不存在的变量中的元素

如果遇到此错误怎么办?

首先,您可能想尝试 which .这将告诉您是否定义了您要使用的函数或变量.

First you might want to try which. This will tell you whether or not the function or variable you're trying to use is defined.

which int
'int' not found.

很明显,Matlab找不到名为int的任何函数或变量.因此,尝试使用它是徒劳的.让我们将其与max:

It's quite obvious, Matlab can't find any functions or variables named int. Trying to use it is therefore futile. Let's compare it to max:

which max
built-in (C:\Program Files\MATLAB\R2014b\toolbox\matlab\datafun\@logical\max)  % logical method

但是,即使您获得了未定义的函数'x'...",也可能会得到以下结果.如果是这样,请参阅下面的第3点.

But, you might get the following, even if you get the "Undefined function 'x' ...". If so, see point 3 below.

which x
x is a variable.

1.但是函数"int"存在!甚至在此处记录 !!

1. But the function "int" exists! It is even documented here!?

是的,int存在,但前提是您具有符号工具箱.由于工具箱是必须单独购买的附加软件包(价格可能非常昂贵),因此您可能没有该软件包.

Yes, int exists, but only if you have the Symbolic Toolbox. Since Toolboxes are additional packages that must be purchased separately (and can be quite expensive), chances are you don't have that package.

如果出现未定义的函数"错误,但在文档中找到了该函数,请在左侧菜单中查看,或者简单地检查地址.标准的Matlab函数具有以下地址:

If you get the "Undefined function" error, but find the function in the documentation, have a look in the menu to the left, or simply check the address. Standard Matlab functions have addresses such as:

mathworks.com/help/matlab/ref/max.html
                   ^^^^^^

注意"matlab"部分.如果看到此信息,则说明您正在使用的功能是Matlab核心的一部分.

Notice the "matlab" part. If you see this, then you are using a function that is part of the core Matlab.

但是,如果看到下面的地址,则您尝试使用的功能是Symbolic Toolbox的一部分:

If however, you see an address such as the one below, then the function you are trying to use is part of the Symbolic Toolbox:

mathworks.com/help/symbolic/int.html
                   ^^^^^^^^

或者它可能是神经网络工具箱的一部分:

or maybe it's part of the Neural Network Toolbox:

mathworks.com/help/nnet/ref/train.html
                   ^^^^ 

解决方案:查找不属于您的工具箱的另一个功能.如果您是熟练的googler,您很可能会找到想要的东西.如果找不到,请寻求帮助.解释一下您尝试过的内容,并表明您已经做了一些努力!

Solution: Find another function that isn't part of a toolbox you don't have. Chances are you'll find what you are looking for if you are a skilled googler. If you can't find it, ask for help. Explain what you have tried, and show that you have done some effort!

2.但是该函数是在此处记录的,并且是Matlab核心的一部分!

2. But the function is documented here, and is part of core Matlab!?

即使函数是标准Matlab安装的一部分,并且有据可查,您仍然可能会遇到此错误.造成此错误的最可能原因是您使用的是Matlab的旧版本.如果您查看文档,则会在页面底部看到以下内容:

Even though a function is part of the standard Matlab installation, and is well documented, you may still get this error. The most likely cause for this error is that you are using an older version of Matlab. If you check the documentation you'll see the following in the bottom of the page:

R2013b中引入

Introduced in R2013b

因此,如果您使用的是R2012b,则不能使用实例table.

So, if you are using R2012b then you can't use for instance table.

解决方案::检查该功能是否在您的Matlab版本中定义.如果尚未实现,则您需要更新Matlab或找到另一种方法. table的替代方法例如可以是使用单元格或结构.

Solution: Check if the function is defined in your version of Matlab. If it's not yet implemented then you either need to update Matlab or find another way to do it. An alternative to table can for instance be to use cells or structs.

3.但是变量"my_variable"存在!我是在上面的行中创建的!

不,您没有.您可能会创建变量myvariablemy_Variablemy_Variable或上一行中的类似内容.您也可能已经创建了它,但是却意外地 cleared 它.

No, you didn't. Chances are you created the variable myvariable, my_Variable, my_Variable or something similar in the line above. It's also possible that you have created it, but have accidentally cleared it.

解决方案:浏览代码.查找拼写错误,不小心清除了变量的位置等.在Matlab编辑器中,如果标记了变量,则会在底部显示以下行:如果定义并使用了函数,则找到3种"x"用法" .您还会在未使用的变量下看到一条橙色线.

Solution: Go through the code. Look for typos, places where you have accidentally cleared the variable etc. Inside the Matlab editor you will get the following line in the bottom if you mark a variable: "3 usages of "x" found" if you have defined and used the function. You will also get an orange line underneath variables that are unused.

4.但是当我输入which y时,我得到"y是一个变量"?

4. But I get "y is a variable" when I type which y?

如果在未定义函数'y'上方出现错误,但which告诉您y存在,则错误消息中还包含几行:

If you get the error above "Undefined function 'y', but which tells you y exists, your error message contains a few more lines:

my_function(x)
Undefined function or variable 'y'.
Error in my_function (line 2)
t = x*y; 

>> which y
y is a variable.

这说明您在 Matlab工作区(另请参见此链接).

What this tells you is that you have a variable called y in your Matlab Workspace (also check this link).

问题在于函数无法访问此工作空间:

The problem is that functions can't access this workspace:

函数不使用基本工作空间.每个函数都有其自己的函数工作区.

Functions do not use the base workspace. Every function has its own function workspace.

如果要让函数查看和使用变量,则必须将其作为

If you want a function to see and use a variable, you must pass it as an argument. This way the variable will be part of the local workspace for that function. Similarly, if you want variables created inside the function to be accessible outside of the function you must have it as output from the function.

解决方案::将要用作输入参数的变量传递给所使用的函数.确保函数内部的名称在内部是一致的. IE.在整个函数中,它必须具有相同的名称.请注意,函数外部和内部的变量名称之间没有任何联系.

Solution: Pass the variables you want to use as input arguments to the function you use. Make sure the names inside the functions are internally consistent. I.e. it must have the same name throughout the function. Note that there is no connection between the variable names outside and inside the function.

5.但是我将变量作为函数的输入传递,但仍然收到相同的错误消息!?

是的,您可能使用了变量作为输入.但是,变量名称在不同的函数中不一定相同(大多数情况下不是一样).

Yes, you probably use the variable as input. However, the variable names are not necessarily the same in different functions (most often they are not).

假设您具有以下功能:

function output = my_function(x)  
output = 2*y;
end

即使使用y作为输入变量,并在函数内部使用y,即使从工作区中按以下代码调用它,也会遇到与上述相同的错误.

You'll get the same error as above if you call it from the workspace as in the code below, even though you are using y as input variable, and use y inside the function.

y = 3;
res = my_function(y)

这是因为在函数my_function中,用作输入的变量将被称为x,而不管在函数外部被称为什么.

This is because inside the function my_function, the variable you use as input will be called x, regardless of what it was called outside the function.

解决方案::在函数头中更改输入变量名称,或在整个函数中更改变量名称.

Solution: Change the name of the input variable name in the function header, or change the name of the variable throughout the function.

6.但是我已经将x创建为全局变量!?

6. But I have created x as a global varible!?

首先,我建议您不要使用全局变量!最好将变量作为参数传递.

First off: I recommend you to not use global variables! It's much better to pass variables as arguments.

在Matlab中将变量声明为全局是不够的工作区.必须在使用它的每个函数中声明它.因此,如果您有全局变量x,则需要在每个函数中执行global x.

It's not enough to declare a variable as global in the Matlab workspace. It must be declared in every function you use it in. So, if you have a global variable x, you need to do global x in every function.

解决方案::重写代码并将变量作为参数传递,而不要使用全局变量.如果这不是一个选择,请在使用它的所有功能中添加global x.

Solution: Rewrite your code and pass variables as arguments instead of using global variables. If this is not an option, add global x in all functions where you're using it.

除了此答案外,您还可以参考官方的 Matlab常见问题解答.

In addition to this answer, you can refer to the official Matlab FAQ.

这篇关于“类型为"double"的输入参数的未定义函数"function_name"."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆