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'.
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.
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'.
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?
Instead of duplicating the whole
fminbnd
function, try:this will work as an "alias" to the existing function:
(you can get the other output arguments as well)
Doing a
which train
showed me that there is a function in the neural network toolbox of the same name.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:
Maybe you can name your file something else like
myfminbnd.m
instead?