feval

Calls a function given by handle or by name.

Category: Language Fundamentals > Data Types

Syntax

[y1, ..., yN] = feval(fun, x1, ..., xM)

Calls fun with the arguments x1 to xM and returns its outputs.

Description

feval(@f, a, b) is the same as f(a, b). It is useful when the function to run is chosen at run time, for example read from a variable or a struct field. With a handle, calling fun(a, b) directly is usually clearer.

Input arguments

fun
Function handle, or the name of a function as text.
x1, ..., xM
Arguments passed on to fun.

Output arguments

y1, ..., yN
Outputs of fun. The number requested is passed on, so [m, i] = feval(@max, v) gets both outputs.

Example

By name and by handle

feval('max', [1 5 2])
feval(@(a, b) a + b, 2, 3)
[m, i] = feval(@max, [4 9 2])
Output
ans = 5
ans = 5
m = 9
i = 2

See also

func2str str2func arrayfun cellfun functions