What does this matlab statement do

2 views (last 30 days)
i have a statement in my matlab program:
f = @(A)DistanceGauss(A,x_axis,Y_target,Y_initial,numOf,modus);
I understood that : f is defined as the function handle to the function distancegauss which contains the paramenters/arg list present inside the paranthesis.
what does the variable "A" in @(A) do? does it have any imporatance...while browsing i found that the variables within paranthesis after @ would be the input arguments for an anonymous function..
can anyone explain what does that "A" do? will this handle work even without that "A" after @ symbol ?, because it is already present as an arg to be passed after the function name.

Accepted Answer

Adam
Adam on 14 Dec 2015
Edited: Adam on 14 Dec 2015
The variables within the first set of brackets, i.e. 'A' in this case are arguments that are to be specified at call time i.e. you call f with the syntax
f(A)
All the other arguments (in the second set of brackets) must be known/available at the time that the function handle is created. e.g.
f = @(x,y) someFunc(x,y,z,w)
would require that 'z' and 'w' are in the workspace at the time that statement is evaluated, whereas 'x' and 'y' are place holders for variables to be supplied at call time as above.
Notice that the variables in the first set of brackets are also passed to the second set of brackets (i.e. arguments to the function).
This does not have to be the case, e.g. in callback functions where 'source' and 'event data' are always supplied. If you do not pass the arguments to the actual function in the second set of brackets then they must still be supplied at call time, but they get thrown away e.g.
f = @(src,evt) myFunction( obj )
may be quite common as a callback syntax because you aren't interested in the 'src' and 'evt' parameters passed down, but you have to acknowledge that they will be passed in at call time otherwise you don't have valid syntax for a callback function.
  3 Comments
Stephen23
Stephen23 on 14 Dec 2015
Edited: Stephen23 on 14 Dec 2015
Why not read the documentation? Any internet search engine will return you the anonymous function's help page, and then simply click on the contents "Functions with No Inputs". This is what it says:
"If your function does not require any inputs, use empty parentheses when you define and call the anonymous function. For example:"
>> t = @() datestr(now);
>> d = t()
d =
26-Jan-2012 15:11:47
So if the variable A already exists and is not an input to the anonymous function, then you can use this syntax:
f = @() DistanceGauss(A,x_axis,Y_target,Y_initial,numOf,modus);
Documentation: it tells us all how to use MATLAB.
Adam
Adam on 14 Dec 2015
"Even if i call 'f' without any arguement like for eg. a=f, the function handle f will still take A as a parameter?"
Just calling f without passing 'A' in would be valid if your function did not use 'A' anywhere, but that isn't what you want. It wouldn't suddenly go back and pick up 'A' from the workspace in which it was defined.
This is true of any function, whether anonymous, via a function handle or just a standard function. i.e. if you have a function with syntax
function myFunc( x, y, z )
then
myFunc( x, y, z )
myFunc( x, y )
myFunc( x )
myFunc( )
are all valid calling syntaxes for the function, but unless the arguments you don't pass in are optional, with the function checking for their existence and creating defaults (or if it is a sloppily written function that takes an argument it never uses), then the function will crash at whatever line first attempts to use the undefined parameter.
This is why I always validate inputs to my functions at the top so it can't get beyond validation and crash at some 'arbitrary' line of the function if I pass in incorrect arguments or not enough arguments.

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation 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!