Can somebody explain to me the meaning of the @
(function handle) operator and why to use it?
相关问题
- Extract matrix elements using a vector of column i
- Keeping track of variable instances
- How to get the maximum of more than 2 numbers in V
- F#: Storing and mapping a list of functions
- How do you get R's null and residual deviance
相关文章
- Accessing an array element when returning from a f
- How do I append metadata to an image in Matlab?
- Equivalent to window.setTimeout() for C++
- How can I write-protect the Matlab language?
- Java Equivalent to iif function
- How do you create a formula that has diminishing r
- `std::sin` is wrong in the last bit
- Escape sequence to display apostrophe in MATLAB
Function handles are an extremely powerful tool in matlab. A good start is to read the online help, which will give you far more than I can. At the command prompt, type
A function handle is a simple way to create a function in one line. For example, suppose I wished to numerically integrate the function sin(k*x), where k has some fixed, external value. I could use an inline function, but a function handle is much neater. Define a function
See that I can now evaluate the function fofx at the command line. MATLAB knows what k is, so we can use fofx as a function now.
In fact, we can pass fofx around, effectively as a variable. For example, lets call quad to do the numerical integration. I'll pick the interval [0,pi/2].
As you can see, quad did the numerical integration. (By the way, an inline function would have been at least an order of magitude slower, and far less easy to work with.)
By way of comparison, try an inline function.
A neat thing about a function handle is you can define it on the fly. Minimize the function cos(x), over the interval [0,2*pi]?
There are many, many other uses for function handles in MATLAB. I've only scratched the surface here.
Disclaimer: code not tested...
The function handle operator allows you to create a reference to a function and pass it around just like any other variable:
Once you've got a function handle, you can invoke it just like a regular function:
One nice thing about function handles is that you can pass them around just like any other data, so you can write functions that act on other functions. This often allows you to easily separate out business logic:
The Matlab documentation has a fuller description of the Matlab syntax, and describes some other uses for function handles like graphics callbacks.
The function handle operator in MATLAB acts essentially like a pointer to a specific instance of a function. Some of the other answers have discussed a few of its uses, but I'll add another use here that I often have for it: maintaining access to functions that are no longer "in scope".
For example, the following function initializes a value
count
, and then returns a function handle to a nested functionincrement
:Since the function
increment
is a nested function, it can only be used within the functionstart_counting
(i.e. the workspace ofstart_counting
is its "scope"). However, by returning a handle to the functionincrement
, I can still use it outside ofstart_counting
, and it still retains access to the variables in the workspace ofstart_counting
! That allows me to do this:Notice how we can keep incrementing count even though we are outside of the function
start_counting
. But you can do something even more interesting by callingstart_counting
again with a different number and storing the function handle in another variable:Notice that these two different counters operate independently. The function handles
fh
andfh2
point to different instances of the functionincrement
with different workspaces containing unique values forcount
.In addition to the above, using function handles in conjunction with nested functions can also help streamline GUI design, as I illustrate in this other SO post.