Create Function Handle - MATLAB & Simulink - MathWorks Deutschland (2024)

Create Function Handle

What Is a Function Handle?

A function handle is a MATLAB® data type that stores anassociation to a function. Indirectly calling a function enables youto invoke the function regardless of where you call it from. Typicaluses of function handles include:

  • Passing a function to another function (often called function functions). For example, passing a function to integration and optimization functions, such as integral and fzero.

  • Specifying callback functions (for example, a callback that responds to a UI event or interacts with data acquisition hardware).

  • Constructing handles to functions defined inline instead of stored in a program file (anonymous functions).

  • Calling local functions from outside the main function.

You can see if a variable, h, is a functionhandle using isa(h,'function_handle').

Creating Function Handles

To create a handle for a function, precede the function namewith an @ sign. For example, if you have a functioncalled myfunction, create a handle named f asfollows:

f = @myfunction;

You call a function using a handle the same way you call thefunction 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 squareof four.

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

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

h = @ones;a = h()
a = 1

Without the parentheses, the assignment creates another functionhandle.

a = h
a = @ones

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

q = integral(f,0,1);

Function handles store their absolute path, so when you havea valid handle, you can invoke the function from any location. Youdo not have to specify the path to the function when creating thehandle, 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 specifiedby namelengthmax. Otherwise, MATLAB truncatesthe latter part of the name.

  • Scope — The function must be in scope at thetime you create the handle. Therefore, the function must be on the MATLAB pathor 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 functionswith the same name, MATLAB uses the same precedence rules todefine function handles as it does to call functions. For more information,see Function Precedence Order.

  • Overloading — When a function handle is invoked with one or more arguments, MATLAB determines the dominant argument. If the dominant argument is an object, MATLAB determines if the object’s class has a method which overloads the same name as the function handle’s associated function. If it does, then the object’s method is invoked instead of the associated function.

Anonymous Functions

You can create handles to anonymous functions. An anonymousfunction is a one-line expression-based MATLAB function thatdoes not require a program file. Construct a handle to an anonymousfunction 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 ananonymous function that computes the square of a number, and callthe 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 theminto 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 saves the absolute path information. You can invoke the function from any location that MATLAB is able to reach, as long as the file for the function still exists at this location. 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

str2func | func2str | functions | isa | function_handle

Related Examples

  • Pass Function to Another Function

More About

  • Anonymous Functions
Create Function Handle
- MATLAB & Simulink
- MathWorks Deutschland (2024)
Top Articles
Depth of Market (DOM) trading is now supported through CQG on TradingView!
Delta — Indikatoren und Signale — TradingView
Chs.mywork
Hotels Near 625 Smith Avenue Nashville Tn 37203
Main Moon Ilion Menu
Www.fresno.courts.ca.gov
Tabc On The Fly Final Exam Answers
Federal Fusion 308 165 Grain Ballistics Chart
Wmu Course Offerings
Ashlyn Peaks Bio
Lowes 385
Displays settings on Mac
AB Solutions Portal | Login
Ecers-3 Cheat Sheet Free
Caresha Please Discount Code
Studentvue Columbia Heights
Alexander Funeral Home Gallatin Obituaries
Xomissmandi
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Milspec Mojo Bio
Candy Land Santa Ana
Delaware Skip The Games
12 Top-Rated Things to Do in Muskegon, MI
Craigslist Org Appleton Wi
Hannaford To-Go: Grocery Curbside Pickup
Minnick Funeral Home West Point Nebraska
Slim Thug’s Wealth and Wellness: A Journey Beyond Music
Naya Padkar Gujarati News Paper
Deshuesadero El Pulpo
Suspiciouswetspot
Cosas Aesthetic Para Decorar Tu Cuarto Para Imprimir
100 Gorgeous Princess Names: With Inspiring Meanings
Uky Linkblue Login
Mp4Mania.net1
Rogers Centre is getting a $300M reno. Here's what the Blue Jays ballpark will look like | CBC News
Plead Irksomely Crossword
Felix Mallard Lpsg
The Closest Walmart From My Location
Lovely Nails Prices (2024) – Salon Rates
10 Rarest and Most Valuable Milk Glass Pieces: Value Guide
Go Bananas Wareham Ma
Isabella Duan Ahn Stanford
Ethan Cutkosky co*ck
Hkx File Compatibility Check Skyrim/Sse
Craigslist Rooms For Rent In San Fernando Valley
Mynord
Yourcuteelena
How the Color Pink Influences Mood and Emotions: A Psychological Perspective
Lesly Center Tiraj Rapid
Leland Westerlund
Underground Weather Tropical
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5477

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.