Wednesday, November 1, 2017

[Matlab GUI] About function in GUI (Mathworks link)

1. Create Function Handle
source link

You can create function handles to named and anonymous functions. You can store multiple function handles in an array, and save and load them, as you would any other variable.



What Is a Function Handle?

A function handle is a MATLAB® data type that stores an association to a function. Indirectly calling a function enables you to invoke the function regardless of where you call it from. Typical uses of function handles include:
  • Pass a function to another function (often called function functions). For example, passing a function to integration and optimization functions, such as integral and fzero.
  • Specify callback functions. For example, a callback that responds to a UI event or interacts with data acquisition hardware.
  • Construct handles to functions defined inline instead of stored in a program file (anonymous functions).
  • Call local functions from outside the main function.
You can see if a variable, h, is a function handle using isa(h,'function_handle').

Creating Function Handles

To create a handle for a function, precede the function name with an @ sign. For example, if you have a function called myfunction, create a handle named f as follows:
f = @myfunction;
You call a function using a handle the same way you call the function directly. For example, suppose that you have a function named computeSquare, defined as:

function y = computeSquare(x)
y = x.^2;
end


Create a handle and call the function to compute the square of four.

f = @computeSquare;
a = 4;
b = f(a)

b =

    16
If the function does not require any inputs, then you can call the function with empty parentheses, such as

h = @ones;
a = h()

a =

    1
Without the parentheses, the assignment creates another function handle.

a = h

a = 

    @ones
Function handles are variables that you can pass to other functions. For example, calculate the integral of x2 on the range [0,1].

q = integral(f,0,1);

Function handles store their absolute path, so when you have a valid handle, you can invoke the function from any location. You do not have to specify the path to the function when creating the handle, only the function name.
Keep the following in mind when creating handles to functions:
  • Name length — Each part of the function name (including package and class names) must be less than the number specified by namelengthmax. Otherwise, MATLAB truncates the latter part of the name.
  • Scope — The function must be in scope at the time you create the handle. Therefore, the function must be on the MATLAB path or in the current folder. Or, for handles to local or nested functions, the function must be in the current file.
  • Precedence — When there are multiple functions with the same name, MATLAB uses the same precedence rules to define function handles as it does to call functions. For more information, see Function Precedence Order.
  • Overloading — If the function you specify overloads a function in a class that is not a fundamental MATLAB class, the function is not associated with the function handle at the time it is constructed. Instead, MATLAB considers the input arguments and determines which implementation to call at the time of evaluation.

Anonymous Functions

You can create handles to anonymous functions. An anonymous function is a one-line expression-based MATLAB function that does not require a program file. Construct a handle to an anonymous function by defining the body of the function, anonymous_function, and a comma-separated list of input arguments to the anonymous function, arglist. The syntax is:
h = @(arglist)anonymous_function
For example, create a handle, sqr, to an anonymous function that computes the square of a number, and call the anonymous function using its handle.

sqr = @(n) n.^2;
x = sqr(3)

x =

     9
For more information, see Anonymous Functions.

Arrays of Function Handles

You can create an array of function handles by collecting them into a cell or structure array. For example, use a cell array:

C = {@sin, @cos, @tan};
C{2}(pi)

ans =

    -1
Or use a structure array:

S.a = @sin;  S.b = @cos;  S.c = @tan;
S.a(pi/2)

ans =

     1

Saving and Loading Function Handles

You can save and load function handles in MATLAB, as you would any other variable. In other words, use the save and load functions. If you save a function handle, MATLAB does not save the path information. If you load a function handle, and the function file no longer exists on the path, the handle is invalid. An invalid handle occurs if the file location or file name has changed since you created the handle. If a handle is invalid, MATLAB might display a warning when you load the file. When you invoke an invalid handle, MATLAB issues an error.

See Also



2. Pass Function to Another Function
You can use function handles as input arguments to other functions, which are called function functions . These functions evaluate mathematical expressions over a range of values. Typical function functions include integralquad2dfzero, and fminbnd.
For example, to find the integral of the natural log from 0 through 5, pass a handle to the log function to integral.

a = 0;
b = 5;
q1 = integral(@log,a,b)

q1 = 3.0472
Similarly, to find the integral of the sin function and the exp function, pass handles to those functions to integral.

q2 = integral(@sin,a,b)

q2 = 0.7163

q3 = integral(@exp,a,b)

q3 = 147.4132
Also, you can pass a handle to an anonymous function to function functions. An anonymous function is a one-line expression-based MATLAB® function that does not require a program file. For example, evaluate the integral of  on the range [0,Inf]:

fun = @(x)x./(exp(x)-1);
q4 = integral(fun,0,Inf)

q4 = 1.6449
Functions that take a function as an input (called function functions) expect that the function associated with the function handle has a certain number of input variables. For example, if you call integral or fzero, the function associated with the function handle must have exactly one input variable. If you call integral3, the function associated with the function handle must have three input variables. For information on calling function functions with more variables, see Parameterizing Functions.

Related Examples


No comments:

Post a Comment