How can I pass a function into another in matlab:
For example assume this function works as optimizer :
Function [returnValue]=optimizer(@myfunction)
%function definition
End
How can I call optimizer function to optimize myfunction?
Functions can be passed into other functions Functions, like any other object, can be passed as an argument to another function.
If you put those two functions in a function file and try to call hahaha from the MATLAB prompt, MATLAB will error. Only the main function in a function file (the first one in the file) is directly callable by code outside that file. f would be a function handle to the local function.
repeat( action , n ) repeats the same action n times. You can specify the input arguments in any order. That is, repeat(action,n) and repeat(n,action) both repeat the action n times.
You can do as follow:
function optimizer(f)
...
x=0;
y=f(x);
...
end
And you call it like that:
f=@(x) (x^2);
optimizer(f)
Matlab has function handles which enables you to pass function pointers around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With