matlab - 强制评估 MATLAB 匿名函数中的变量

标签 matlab anonymous-function function-handle

MATLAB 将变量与匿名函数一起存储。这是一个来自 the documentation 的工作原理示例.

Variables in the Expression:

Function handles can store not only an expression, but also variables that the expression requires for evaluation.

For example, create a function handle to an anonymous function that requires coefficients a, b, and c.

a = 1.3;
b = .2;
c = 30;
parabola = @(x) a*x.^2 + b*x + c;

Because a, b, and c are available at the time you create parabola, the function handle includes those values. The values persist within the function handle even if you clear the variables:

clear a b c
x = 1;
y = parabola(x)
y =
   31.5000

据推测,a b 和 c 的值与函数一起存储,即使它是从 mat 文件中保存和重新加载的。在实践中,我发现这些值不会持续存在,尤其是在最初创建该函数的代码被编辑的情况下。

有没有办法根据变量的数值来定义函数句柄?我想要某种形式的东西

>> a = 1.3;
>> b = .2;
>> c = 30;
>> parabola = @(x) a*x.^2 + b*x + c

parabola = @(x) a*x.^2+b*x+c

>> parabola2 = forceEval(parabola)

parabola2 = @(x) 1.3*x.^2+.2x+30

编辑:也许我的问题出在文件关联上,但是当我编辑我最初在其中定义匿名函数的文件时,出现如下错误:

无法找到函数@(ydata)nr/(na*dt)*normpdf(ydata,mu(j),s(j))./normpdf(ydata,mu_a(j),s_a(j)) C:...\m文件名.m。 (我将 mfile 的名称更改为 mfilename)

对于这类问题,我通常的解决方案是使用 func2str() 来删除文件依赖性,但这也会删除包括参数值在内的工作区信息。所以我想强制所有参数在函数定义中采用它们的数值。

最佳答案

值存储在函数中。正如我之前在不同答案中所展示的那样,您可以使用 functions 命令进行检查:

>> a = 1.3; b = .2; c = 30;
>> parabola = @(x) a*x.^2 + b*x + c;
>> x = 1;
>> y = parabola(x)
y =
         31.5
>> clear a b c
>> y = parabola(x)
y =
         31.5
>> fi = functions(parabola)
fi = 
     function: '@(x)a*x.^2+b*x+c'
         type: 'anonymous'
         file: ''
    workspace: {[1x1 struct]}
>> fi.workspace{1}
ans = 
    a: 1.3
    b: 0.2
    c: 30

即使您将句柄保存到磁盘:

>> save parabolaFun.mat parabola
>> clear parabola a b c
>> load parabolaFun.mat parabola
>> y = parabola(x)
y =
         31.5
>> fi = functions(parabola)
fi = 
     function: '@(x)a*x.^2+b*x+c'
         type: 'anonymous'
         file: ''
    workspace: {[1x1 struct]}
>> fi.workspace{1}
ans = 
    a: 1.3
    b: 0.2
    c: 30

您可以像这样简化抛物线句柄的创建:

function p = makeParabola(a,b,c)

p = @(x) a*x.^2 + b*x + c;

end

certain caveats :

You can save and load function handles in a MAT-file using the MATLAB® save and load functions. If you load a function handle that you saved in an earlier MATLAB session, the following conditions could cause unexpected behavior:

  • Any of the files that define the function have been moved, and thus no longer exist on the path stored in the handle.
  • You load the function handle into an environment different from that in which it was saved. For example, the source for the function either does not exist or is located in a different folder than on the system on which the handle was saved.

In both of these cases, the function handle is now invalid because it is no longer associated with any existing function code. Although the handle is invalid, MATLAB still performs the load successfully and without displaying a warning. Attempting to invoke the handle, however, results in an error.

因此,如果您从文件支持的函数(不是脚本,没关系)创建句柄,然后修改或删除文件,句柄将变得无效。

关于matlab - 强制评估 MATLAB 匿名函数中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808668/

相关文章:

matlab - 如何使用 MATLAB 计算二值图像中白色像素的数量?

matlab - 如何使用 matlab 从 FAR/FRR 值计算和绘制等错误率 (EER)

Scala匿名函数语法和返回类型

string - 将匿名函数扩展为字符串

matlab - 如何访问元胞数组中的函数句柄?

function - 如果不使用引号 '' ,将函数作为参数传递给另一个函数将无法编译?

python正弦和余弦精度

c# - 不能通过委托(delegate)

built-in - 如何获取覆盖的内置函数的句​​柄?

algorithm - MATLAB:快速创建具有固定度数(行总和)的随机对称矩阵