无法通过函数句柄作为函数的参数 [英] Cannot pass function handle as an argument of a function

查看:1158
本文介绍了无法通过函数句柄作为函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Matlab和我想要编写自定义函数在MATLAB,将采取函数句柄作为它的一个参数。
我得到这个错误的所有时间:

I'm new to Matlab and I'm trying to write custom function in matlab that would take function handle as one of its arguments. I'm getting this error all the time:

Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.

试图调试我进行以下测试:我运行命令 X = fminbnd(@humps,0.3,1)。我继续如预期 - 我得到的结果 X = 0.6370
所以,我创建了一个名为列车自定义函数,我复制的所有的功能code fminbnd 到文件 train.m 。我唯一​​改变的是名称,这样的函数,code fminbnd 列车现在是相同的除了名称

Trying to debug I performed following test: I run command x = fminbnd(@humps, 0.3, 1). I proceeded as expected - I got result x = 0.6370. So I created custom function called train and I copied ALL the code of function fminbnd to the file train.m. The only thing that I changed is the name, so that code of functions fminbnd and train is now identical except for the names.

现在我使用相同的参数同时运行的功能和自定义功能会抛出错误,而原始的 fminbnd 返回正确的答案。
这里是code:

Now I run both functions with the same argument and the custom function throws error while original fminbnd returns correct answer. Here is the code:

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

Here is header of function train (everything else is copied from fminbnd):

function [xf,fval,exitflag,output] = train(funfcn,ax,bx,options,varargin)

问题出在哪里?

Where is the problem?

推荐答案

做一个这列车给我看,有同名的神经网络工具箱功能。

Doing a which train showed me that there is a function in the neural network toolbox of the same name.

/Applications/MATLAB_R2009b.app/toolbox/nnet/nnet/@network/train.m  % network method

您可能会运行NNET train.m,而不是你认为你正在运行的之一。您在包含train.m的目录?当我确信我是在正确的目录,我得到了它工作:

You may be running the nnet train.m rather than the one you think you're running. Are you in the directory containing your train.m? When I made sure I was in the right directory, I got it to work:

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

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

x =

    0.6370

也许你可以命名你的文件别的东西像 myfminbnd.m 呢?

这篇关于无法通过函数句柄作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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