Functions as function inputs with specified inputs

I am trying to create a code e.g.:
function Y=evaluator(a,b,c,d,e,f,g,function1)
Y=function1(a,b,c,d,e,f,g);
end
such that I can input any .m file as an input argument (for function1) and it would be evaluated with the input arguments (a,b,c,d,e,f,g). In other words, I want to have the code evaluate (say) any ..m file that has 7 input arguments in this code.
My issue is that I get the "Not enough input arguments" Error every time I try to run this, even though function11.m has 7 input variables. Yet if I call up the .m files from the Command Window - i.e. function11(a,b,c,d,e,f,g) - it runs perfectly. What's more, changing Y=function1(a,b,c,d,e,f,g); back to Y=function11(a,b,c,d,e,f,g); (the actual .m file name - as I had before trying to do this, which worked before), the error still pops up and there is nothing I can do about it.
I have also tried using:
Y=feval(function1(a,b,c,d,e,f,g));
with the same result.
Any suggestions/comments?

5 Comments

Y=feval(function1(a,b,c,d,e,f,g));
evaluates function1(a,b,c,d,e,f,g) at first and provide the output to the feval() command. Most likely you mean:
feval(function1, a,b,c,d,e,f,g)
if function1 is a function_handle.
Thank you! This works when I type the actual function in, but if I have it like so:
function Y=evaluator(a,b,c,d,e,f,g,funct)
Y=(funct,a,b,c,d,e,f,g);
end
and I input (say) evaluator(1,2,3,4,5,6,7,function1) it still outputs the error. Writing feval(function1,1,2,3,4,5,6,7) into the command window works perfectly well...
I looked function handles up and it tells me to use feval(@funct,a,b,c,d,e,f,g); However this only works if funct is the name of the function - and does not work if I use funct as an arbitrary input argument for the code.
Any suggestions?
"Any suggestions?"
  • Do not use strings to access functions.
  • Create function handles right from the beginning.
  • Pass the function handle that is required.
  • Call the handle directly, you do not need feval.
"does not work if I use funct as an arbitrary input argument for the code"
It does work if funct is a function handle to any arbitrary function that you have defined. This will work:
feval(funct,a,b,c,d,e)
but can be easily simplified by calling the handle directly:
funct(a,b,c,d,e)
A function handle can be a handle to any arbitrary function: see the last example of my answer. It is not clear why you think that function handles cannot be arbitrary functions: they can be handled just like any other variable, so you can pass them around and rename them. You could even create a cell array of function handles and loop over them:
>> C = {@sin,@cos,@sqrt};
>> for k = 1:numel(C), fun=C{k}; fun(0.5), end
ans = 0.47943
ans = 0.87758
ans = 0.70711
Note that the loop does not refer to any specific function at all.
I would highly recommend that you read some more about function handles, do some tutorials on them, and practice using them.
Yan, Your missing the function handle point. Try
evaluator(1,2,3,4,5,6,7,@function1)
So that a function handle is passed to evaluator instead of attempting to call function1 with no input arguments before even calling evaluator.
Perfect. Function handles worked. (tried using them before but used them wrong)
Thank you!!!

Sign in to comment.

 Accepted Answer

You need to pass the function handle, because what you are doing now calls the function when you try to input it to evaluator. For example you could do this, where fun is the name of your function:
evaluator(...,@fun)
not this:
evaluator(...,fun)
Also note that you could easily write evaluator so that it accepts any number of input arguments:
>> evaluator = @(fun,varargin)fun(varargin{:});
>> evaluator(@max,[1,2,3],2)
ans =
2 2 3
Although to be honest writing a special wrapper function to evaluate functions is a total waste of time: once you have a function handle you can just as easily call that handle directly, no wrapper is required:
>> fun = @max;
>> fun([1,2,3],2)
ans =
2 2 3
You would be much better off learning how to use function handles properly, rather than wasting your time with this pointless evaluator function: function handles can be evaluated directly, they do not need some special wrapper to do it. Use function handles: that is exactly what they are for!

2 Comments

Thank you for that. Have to point out that my actual code is a lot more complex than the example I gave in the question. My code is designed to solve ODE's and PDE's numerically using various coarse and fine solvers.
The reason for me trying to implement the above is that I want to be able to write an .m file for different solvers and to be able to implement them in the code without having to rewrite the code every time I consider a different numerical solver. Hence me trying to do this.
"I want to be able to write an .m file for different solvers and to be able to implement them in the code without having to rewrite the code every time I consider a different numerical solver"
This is exactly why function handles are useful, because you do not need to rewrite the code for different functions: just pass a different function handle.
Whatever you do, do not pass functions as strings: this is a slow and primitive way of manipulating functions. You would be much better off generating function handles right from the start and passing them to whatever code or solvers you have.
And your wrapper is a waste of time. Use function handles.

Sign in to comment.

More Answers (1)

  • See Create Function Handle. Very useful and worth spending some time to learn.
  • The input argument, function1, shall IMO be replaced by a function handle.
Did you try
Y = feval( function1, a,b,c,d,e,f,g );
Doc says:
Syntax
[y1,...,yN] = feval(fun,x1,...,xM)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!