Written and maintained by CASRAI Editorial Board
Last updated
Short answer: MATLAB’s equivalent of a lambda function is the anonymous function, created with the @ operator: sqr = @(x) x.^2; defines a function with no name and no file, and sqr(5) returns 25. This guide is for researchers and analysts who need to pass a short function to a solver, integrator or array routine, and shows what an anonymous function can and cannot do. Facts below were read in the MathWorks MATLAB documentation and the GNU Octave and Python documentation on 2026-11-07.
What a MATLAB lambda (anonymous function) is, and what it is used for in research
MATLAB has no keyword called lambda. The same idea, a small unnamed function defined inline, is written @(inputs) expression. The @ creates a function handle, and the inputs go in parentheses straight after it. The MathWorks documentation describes this as an anonymous function and a function handle as the value you store, pass around and call.
In research code the main uses are the ones the documentation names for function handles: passing a function to another function (for example an integrator or a minimiser), and callbacks for user-interface events or hardware. Typical analysis uses are an objective function for a fitting routine, an integrand, or a per-element calculation applied across an array.
Lambda in MATLAB versus Python: the term mapping
| Idea | Python | MATLAB |
|---|---|---|
| Unnamed function | lambda x: x**2 |
@(x) x.^2 |
| Body | One expression | One expression |
| Outer variables | Looked up in the enclosing scope | Captured and stored in the handle when it is created |
| Call | f(5) |
f(5) |
The Python row follows the language reference, which says a lambda expression yields a function object equivalent to a def that returns one expression. The MATLAB capture behaviour is covered under “Capturing variables” below.
Install or access (official source only)
Anonymous functions are part of the base MATLAB language, so no toolbox is needed to define one. MATLAB is obtained from MathWorks (mathworks.com/products/matlab); licence and access terms are set there, and this page does not state them. If you want a free route, see the alternatives table further down.
Step-by-step: define, call and reuse an anonymous function
- Define it with one input. At the command window or in a script, enter
sqr = @(x) x.^2;. The variablesqrnow holds a function handle. - Call it.
a = sqr(5)returnsa = 25. Use the element-wise operator.^ifxmay be an array. - Add inputs.
myfunction = @(x,y) (x^2 + y^2 + x*y);then, withx = 1andy = 10,z = myfunction(x,y)returns111. - Define one with no inputs.
t = @() datestr(now);and call it with empty parentheses:d = t(). Without the parentheses,tby itself is the handle, not a run of the function. - Pass it to another function.
f = @(x) x.^3 + x.^2 + x + 1; integral(f,0,1)returns2.0833. - Nest one inside another.
g = @(c) (integral(@(x) (x.^2 + c*x + 1),0,1)); g(2)returns2.3333. Herecis a parameter of the outer function and is available to the inner one.
Capturing variables: a reproducible mini-experiment
An anonymous function stores the values of any variables that exist at the moment it is created. Changing or clearing those variables afterwards does not change the handle; you create a new handle to use new values. Try it:
a = 2;
f = @(x) a*x;
f(3) % 6
a = 10;
f(3) % still 6, because a = 2 was captured
f = @(x) a*x;
f(3) % 30, the new handle captured a = 10
The documentation also notes that clear on the captured variables does not remove them from the handle, and recommends using only explicit variables when constructing anonymous functions. For research code this is useful: a fitting routine can be given an objective function with fixed data baked in, and the result stays reproducible if you later overwrite the workspace variable.
Using anonymous functions with integral, fminbnd and arrayfun
integral and fminbnd
Both take a function handle. The documentation shows a named handle, f = @sin; fminbnd(f,0,2*pi), and an anonymous one, f = @(x,y)(x.^2 - y.^2). To fix an extra parameter, wrap the function so that it has only the variable the solver varies, as in the nested g(c) example above.
arrayfun
B = arrayfun(func,A) applies func to the elements of A one at a time. The UniformOutput option defaults to true; set it to false to get cell arrays back, which lets results differ in size or type. The documentation’s example is A = arrayfun(@(x) mean(x.f1),S,'UniformOutput',false). An ErrorHandler option takes a function handle; if you omit it, errors are rethrown.
For related MATLAB tasks, see ode45 in MATLAB, integrating in MATLAB, MATLAB equation solvers and loops in MATLAB.
Output and how to read it
- If you see a result such as
ans = 25, the function ran. - If you see a handle displayed instead of a number, you created or displayed a handle; it has not been evaluated (see Troubleshooting).
- Check your numbers against the documented examples:
sqr(5)gives 25, the two-input example gives 111, the cubic integral gives 2.0833, andg(2)gives 2.3333.
Limits of anonymous functions and workarounds
| Limit (from the documentation) | Practical workaround |
|---|---|
The body is a single expression, so you cannot write several statements or an if block inside it. |
Move the logic into a named function (a local function in a script or a function file) and make the handle @myfun, or wrap a call to it: @(x) myfun(x,p). |
| An anonymous function returns one output by default. | If its expression calls a function with several outputs (such as ndgrid or size), request them when you invoke the handle, as in the documentation’s [x,y] = mygrid(pi,2*pi). For several outputs from other expressions, use deal or a named function. |
| Function handles are scalars, so you cannot make an array of them. | Store them in a cell array or struct array and call with f{3}(x,y). |
Avoid eval, evalin, assignin and load in the body. |
Do that work outside the handle and pass the result in as a captured variable or an input. |
Troubleshooting
I get a handle printed instead of a number
You called the name without parentheses. A no-input function must be called as t().
The function ignores my new variable value
The old value was captured when the handle was created. Redefine the handle after changing the variable.
I want an if statement or several lines inside the function
The body must be one expression. Use a named function instead, as in the table above.
I want two outputs
An anonymous function returns one output by default. If its expression calls a function with several outputs (such as ndgrid or size), you can request them with [x,y] = h(...). Otherwise use deal or a named function.
Free alternatives
| Tool | Equivalent | What the official documentation says |
|---|---|---|
| GNU Octave | @(argument-list) expression |
Same @ syntax; variables not in the argument list are inherited from the enclosing scope. Example: f = @(x) x.^2; quad (f, 0, 10) gives 333.33. The Octave manual (version 11.3.0 page) adds that handles to existing functions, such as quad (@sin, 0, pi), are better for performance than wrapping them in an anonymous function. |
| Python | lambda parameters: expression |
Yields an unnamed function object, equivalent to a def that returns the expression. |
FAQ
What is a lambda function in MATLAB?
It is an anonymous function: a function without a name or file, written @(x) expression and stored as a function handle.
How do I write a lambda function in MATLAB with several inputs?
List the inputs in the parentheses: @(x,y) (x^2 + y^2 + x*y). With x = 1 and y = 10 it returns 111.
Can a MATLAB lambda return more than one value?
Only if the expression’s own function returns several outputs. An anonymous function returns one output by default, but if its expression calls a function such as ndgrid, you can request multiple outputs when you invoke the handle. Otherwise use deal or a named function.
Does a MATLAB lambda see later changes to a variable?
No. Values present when it is created are stored in the handle; create a new handle to use new values.
How do I use a MATLAB lambda inside arrayfun?
Pass it as the first argument: arrayfun(@(x) mean(x.f1),S,'UniformOutput',false).
Sources
- MathWorks, Anonymous Functions, read 2026-11-07.
- MathWorks, function_handle, read 2026-11-07.
- MathWorks, arrayfun, read 2026-11-07.
- GNU Octave manual, Anonymous Functions, read 2026-11-07.
- Python Language Reference, Lambdas, read 2026-11-07.







