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

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

问题描述

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

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

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

"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.

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

"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 FilesMATLABR2014b	oolboxmatlabdatafun@logicalmax)  % 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 存在,但前提是您有 Symbolic Toolbox.由于工具箱是必须单独购买的附加包(并且可能非常昂贵),因此您可能没有该包.

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.

但是,如果您看到如下所示的地址,则您尝试使用的函数是符号工具箱的一部分:

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
                   ^^^^ 

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

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 编辑器中,如果您标记一个变量,您将在底部看到以下行:如果您已经定义并使用了该函数,则找到x"的 3 个用法.您还将在未使用的变量下方看到一条橙色线.

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',但 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.

这告诉您的是,您的 y 的变量="noreferrer">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 中将变量声明为 global 是不够的工作区.它必须在您使用它的每个函数中声明.因此,如果您有一个全局变量 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.

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

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