Matlab 是否支持函数对象?

Does Matlab support function-objects?

试图弄清楚我是否可以访问我们的 Matlab 脚本中使用的函数对象编程技术。这类似于 .NET's Func type, or Python's function objects。 Matlab 是否首先给出函数 class 对象状态?

Matlab 确实有 function handles which can be passed to other functions. As one example, the function fzero will find the zero-crossing of the function you give as its first argument. Function handles can be stored in variables, cell-arrays or structs. Matlab also has anonymous functions, which are similar to Python's lambda expressions. So it seems that functions in Matlab have all the properties to be considered first class.

一些随机示例:

>> sq = @(x) x^2 - 2
sq = 
    @(x)x^2-2

>> fzero(sq, 1)
ans =
    1.4142

>> class(sq)
ans =
function_handle

>> functions = {@(x) 2 * x, @(y) 3 * y, @exp}
functions = 
    @(x)2*x    @(y)3*y    @exp

>> functions{2}(10)
ans =
    30

>> functions{3}(1)
ans =
    2.7183