Functions as function inputs with specified inputs
Show older comments
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
Jan
on 15 Nov 2017
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.
Yan Yanchuk
on 15 Nov 2017
"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.
Philip Borghesani
on 15 Nov 2017
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.
Yan Yanchuk
on 15 Nov 2017
Accepted Answer
More Answers (1)
per isakson
on 14 Nov 2017
Edited: per isakson
on 14 Nov 2017
- 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
Find more on Function Handles in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!