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:
not this:
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!
Best Answer