MATLAB 匿名函数中变量的强制求值 [英] Force evaluation of variables in MATLAB anonymous function

查看:33
本文介绍了MATLAB 匿名函数中变量的强制求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB 将变量与匿名函数一起存储.以下是文档.

MATLAB stores variables along with anonymous functions. Here is an example of how this works from the documentation.

表达式中的变量:

函数句柄不仅可以存储表达式,还可以存储变量表达式需要求值.

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

例如,创建一个匿名函数的函数句柄,需要系数 a、b 和 c.

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;

因为 a、b 和 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 文件中保存和重新加载的.在实践中,我发现这些值不会持续存在,尤其是在最初创建该函数的代码被编辑的情况下.

Supposedly, the values of a b and c are stored with the function even when it's saved and reloaded from a mat file. In practice, I've found that these values do not persist, especially if the code that originally created the function is edited.

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

Is there a way to define the function handle in terms of the numeric values of the variables? I would like something of the form

>> 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

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

Perhaps my problem is with the file association, but when I edit the file that I originally defined the anonymous function in, I get an error that looks like:

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

Unable to find function @(ydata)nr/(na*dt)*normpdf(ydata,mu(j),s(j))./normpdf(ydata,mu_a(j),s_a(j)) within C:...\mfilename.m. (where I've changed the name of my mfile to mfilename)

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

My usual solution to this kind of stuff has been to use func2str() to remove the file dependency, but this also strips out the workspace information including the parameter values. So I would like to force all the parameters to take on their numerical values in the function definition.

推荐答案

值存储在函数中.正如我之前在不同答案中演示的那样,您可以使用 functions 命令检查这一点:

The values are stored in the function. As I've demonstrated in different answers before, you can check this with the functions command:

>> 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

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

Even when you save the handle to disk:

>> 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

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

And you can simplify the creation of a parabola handle like this:

function p = makeParabola(a,b,c)

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

end

某些注意事项:

您可以使用 MATLAB® 保存和加载函数在 MAT 文件中保存和加载函数句柄.如果您加载在早期 MATLAB 会话中保存的函数句柄,以下情况可能会导致意外行为:

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:

  • 定义函数的任何文件都已被移动,因此不再存在于存储在句柄中的路径上.
  • 您将函数句柄加载到与保存它的环境不同的环境中.例如,该函数的源不存在或位于与保存句柄的系统不同的文件夹中.

在这两种情况下,函数句柄现在无效,因为它不再与任何现有的函数代码相关联.尽管句柄无效,MATLAB 仍会成功执行加载且不显示警告.但是,尝试调用句柄会导致错误.

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.

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

Hence, if you create the handle from a file-backed function (not a script, that's OK), and then modify or delete the file, the handle will become invalid.

这篇关于MATLAB 匿名函数中变量的强制求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆