In MATLAB, the @
symbol is used to create anonymous functions. Anonymous functions can be defined inline, meaning that they can be defined where they are used in an expression rather than in a separate function file.
The general syntax to create an anonymous function is @(arg1, arg2,...)expression
.
For example, to create an anonymous function that squares its input, we can write:
main.m19 chars2 lines
This defines a function called square
that takes a single input argument x
and returns x^2
. We can then call the square
function with a value for x
:
main.m26 chars4 lines
Anonymous functions are very useful when we need to pass a small function as an argument to another function, or when we need to define a function that is used only once.
gistlibby LogSnag