Is it possible to define a function with a function handler as an argument in Matlab?
I've tried with
function x = name(@f,gh)
but I get an error message stating Invalid syntax at '@'.
Is it possible to define a function with a function handler as an argument in Matlab?
I've tried with
function x = name(@f,gh)
but I get an error message stating Invalid syntax at '@'.
You cannot use syntax involving @
in function definition. The anonymous function handle would do the work:
function x = SO_Example(h,gh)
x = h(gh);
And you can call the function as follows:
SO_Example(@(a)a.^2 , 2)
ans = 4
Or like this:
h = @(a)a.^2;
SO_Example(h,2)
ans = 4
Please, see comments for additional explanations