Function 无法将函数句柄作为函数的参数传递

Function 无法将函数句柄作为函数的参数传递,function,matlab,arguments,function-handle,Function,Matlab,Arguments,Function Handle,我是Matlab新手,我正在尝试在Matlab中编写自定义函数,将函数句柄作为其参数之一。 我一直都会遇到这个错误: Error using subsindex Function 'subsindex' is not defined for values of class 'function_handle'. 尝试调试时,我执行了以下测试:我运行命令x=fminbnd(@humps,0.3,1)。我按预期进行-我得到了结果x=0.6370。 因此,我创建了名为train的自定义函数,并将函数f

我是Matlab新手,我正在尝试在Matlab中编写自定义函数,将函数句柄作为其参数之一。 我一直都会遇到这个错误:

Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
尝试调试时,我执行了以下测试:我运行命令
x=fminbnd(@humps,0.3,1)
。我按预期进行-我得到了结果
x=0.6370
。 因此,我创建了名为
train
的自定义函数,并将函数
fminbnd
的所有代码复制到文件
train.m
。我唯一更改的是名称,因此功能代码
fminbnd
train
现在除了名称之外是相同的

现在我用相同的参数运行这两个函数,自定义函数在原始
fminbnd
返回正确答案时抛出错误。 代码如下:

>> x = fminbnd(@humps, 0.3, 1)

x =

    0.6370

>> x = train(@humps, 0.3, 1)
Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
这是功能
列车的标题
(其他所有内容都是从
fminbnd
复制的):


问题在哪里

做了一个
哪个训练
告诉我,在神经网络工具箱中有一个同名的函数

/Applications/MATLAB_R2009b.app/toolbox/nnet/nnet/@network/train.m  % network method
你可能正在运行nnet列车。m而不是你认为正在运行的列车。您是否在包含您的train.m的目录中?当我确定我在正确的目录中时,我让它开始工作:

>> which train
/Users/myuserid/train.m

>> x = train(@humps,0.3,1)

x =

    0.6370

也许您可以将文件命名为其他名称,如
myfminbnd.m

不要复制整个
fminbnd
函数,请尝试:

function varargout = myfminbnd(varargin)
    varargout = cell(1,nargout(@fminbnd));
    [varargout{:}] = fminbnd(varargin{:});
end
这将作为现有功能的“别名”:

>> fminbnd(@(x)x.^3-2*x-5, 0, 2)
ans =
       0.8165

>> myfminbnd(@(x)x.^3-2*x-5, 0, 2)
ans =
       0.8165

(您还可以获得其他输出参数)

您是对的+不管我如何复制函数行为,问题在于@kitchenette指出的函数名称。
>> fminbnd(@(x)x.^3-2*x-5, 0, 2)
ans =
       0.8165

>> myfminbnd(@(x)x.^3-2*x-5, 0, 2)
ans =
       0.8165