![]() % the five times logic again, only the operation itself:įunction I don't even need to explicitly declare a function - this is an % now I can say hello five times if there's something else I want to do five times, I don't have to write This often allows you to easily separate out business logic: % prints hello 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. Operation = you've got a function handle, you can invoke it just like a regular function: operation(10, 20) % returns 30 % this variable now points to the add function The function handle operator allows you to create a reference to a function and pass it around just like any other variable: % function to add two numbers There are many, many other uses for function handles in MATLAB. Minimize the function cos(x), over the interval ? xmin = cos(x),0,2*pi) finline = inline('sin(x*k)','x','k') Ī neat thing about a function handle is you can define it on the fly. (By the way, an inline function would have been at least an order of magitude slower, and far less easy to work with.) x = linspace(0,pi,1000) īy way of comparison, try an inline function. quad(fofx,0,pi/2)Īs you can see, quad did the numerical integration. For example, lets call quad to do the numerical integration. In fact, we can pass fofx around, effectively as a variable. MATLAB knows what k is, so we can use fofx as a function now. See that I can now evaluate the function fofx at the command line. I could use an inline function, but a function handle is much neater. For example, suppose I wished to numerically integrate the function sin(k*x), where k has some fixed, external value. At the command prompt, type doc function_handleĪ function handle is a simple way to create a function in one line. A good start is to read the online help, which will give you far more than I can. 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.įunction handles are an extremely powerful tool in matlab. ![]() The function handles fh and fh2 point to different instances of the function increment with different workspaces containing unique values for count. Notice that these two different counters operate independently. > fh2() % Invoke the second handle to increment > fh() % Invoke the first handle to increment But you can do something even more interesting by calling start_counting again with a different number and storing the function handle in another variable: > fh2 = start_counting(-4) Notice how we can keep incrementing count even though we are outside of the function start_counting. > fh() % Invoke increment function using its handle However, by returning a handle to the function increment, I can still use it outside of start_counting, and it still retains access to the variables in the workspace of start_counting! That allows me to do this: > fh = start_counting(3) % Initialize count to 3 and return handle the workspace of start_counting is its "scope"). Since the function increment is a nested function, it can only be used within the function start_counting (i.e. 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".įor example, the following function initializes a value count, and then returns a function handle to a nested function increment: function fHandle = start_counting(count) The function handle operator in MATLAB acts essentially like a pointer to a specific instance of a function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |