How to Pass Parameters in a MATLAB Functional Handle

by David Wayne Google

The MATLAB programming environment can be an important investment for your business, and function handles let you work efficiently by resolving the scope of a function when you don't want to transfer files between computers. Your project may require you to pass arguments to a function handle, and MATLAB provides ways to define handles with one or more parameters. Function handles can be a building block for the research or technological applications your business provides, and many of the predefined functions in MATLAB take parameterized function handles as arguments.

Launch the MATLAB workspace and click inside the Command Window to begin entering commands. Define a function handle with a list of parameters inside parentheses followed by a one-line function definition. For example, enter the following command: _exp = @(p, x) p.^x; The “@” character tells MATLAB to store a function handle in the reference “_exp” for the code that comes before the semicolon.

Invoke the anonymous function with its handle followed by the arguments to pass. For example, type the following: _exp(2, 3) The MATLAB workspace will automatically evaluate the expression as “two to the power of three” and print the result, which is the integer 8.

Create a function handle to pass to a MATLAB “function function,” which is a function that uses the output of a function handle. For example, create a handle for a curve on a graph, such as the following: squared = @(x) x.^2;

Invoke a function with the function handle you've created, such as the MATLAB “quad” function, which finds the area under the curve between two limits. Use code such as the following: quad(squared, 0, 1) You will immediately see the following output in the MATLAB Command Window: ans = 0.3333

Define a “function function” that takes a function handle and other input parameters to apply to the output of the handle. For example, type the following lines of code: function y = newton(u, v, x) y = fzero(@fofx, x) function y = fofx(x) y = x^3 + u*x + v; end end

Pass arguments to the function handle by invoking the “function function” with the arguments to pass. For example, enter the following command: x = newton(2, 3.5, 0) The function passes the “2” and “3.5” arguments to the function handle and sends the output to the predefined “fzero” function, which approximates the point where the graph crosses the x-axis, using the third argument, “0,” as a starting point for iterating to the answer. You will see the following output in the MATLAB workspace: x = -1.0945


  • If you've created a function handle in a MATLAB file, you don't need the original function on the application path to be able to use the handle. After creating the handle, you can invoke the function in any workspace as long as the file containing the function handle assignment is on the path.

About the Author


Photo Credits

  • Hemera Technologies/ Images
Suggest an Article Correction