matlab可执行文件中的匿名函数 [英] anonymous function in matlab executable

查看:36
本文介绍了matlab可执行文件中的匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我编译它们之前,我有三个可以完美运行的文件.然而,当我编译它们时,matlab 表现得好像我没有包含其中一个文件,即使它包含在部署中.

I have a three files that work perfectly before I compile them. However when I compile them matlab behaves as if I didn't include one of the files even though it is included in the deployment.

function testMain

kuzu = zeros(5,1);
anonymousFunction = testClass.anonymousFunction;
kuzu2 = anonymousFunction(kuzu)

end

classdef testClass
    properties (Constant)
        anonymousFunction = @(x) replaceZeroWithNaN2(x)
    end
end

function output = replaceZeroWithNaN2(input)

input(input==0) = NaN;
output = input;

end

所有文件都在同一目录中.编译后出现以下错误:

All the files are in the same directory. After compilation I get the following error:

未定义函数 'replaceZeroWithNaN2' 用于类型为 'double' 的输入参数

Undefined function 'replaceZeroWithNaN2' for input arguments of type 'double'

推荐答案

当 MATLAB Compiler 将您的代码打包成可执行文件时,它需要包含您的 main 函数所依赖的所有文件.它使用依赖项分析来实现这一点,即遍历代码以查看它依赖哪些东西,以及这些东西依赖哪些东西.

When MATLAB Compiler packages your code into an executable, it needs to include all the files that your main function depends on. It does this using dependency analysis, i.e. walking through the code to see which things it depends on, and which things those things depend on.

有时依赖项分析可能会失败并且会遗漏一些依赖项.例如,如果您的代码调用诸如 eval('myfunction') 之类的东西,它就不会找到 myfunction 作为依赖项.

Sometimes the dependency analysis can fail and it misses some dependencies. For example, if your code calls something like eval('myfunction'), it won't find myfunction as a dependency.

看起来,无论出于何种原因,依赖项分析都没有找到 replaceZeroWithNaN2,并且它没有包含在您的可执行文件中,因此您收到了您看到的错误.您可以通过运行 depfun('testMain.m') 自行检查 - depfun 是 MATLAB 用于查找依赖项的命令,输出显示它正在查找依赖项testClass,但不是 replaceZeroWithNaN2.

It looks like, for whatever reason, the dependency analysis is not finding replaceZeroWithNaN2, and it's not getting included in your executable, so you're getting the error you see. You can check this yourself by running depfun('testMain.m') - depfun is the command MATLAB uses to find dependencies, and the output shows that it's finding the dependency on testClass, but not replaceZeroWithNaN2.

在这种情况下,您可以明确告诉依赖项分析包含一个函数.

In cases like this you can explicitly tell the dependency analysis to include a function.

testClass.m的顶部添加以下注释:

Put the following comment at the top of testClass.m:

%#function replaceZeroWithNaN2

%#function 是一个pragma,它明确地告诉依赖分析下面的代码依赖于函数replaceZeroWithNaN2.当我这样做时,depfun 的输出现在包括 replaceZeroWithNaN2.

The %#function is a pragma that explicitly tells the dependency analysis that the code below depends on the function replaceZeroWithNaN2. When I do that, the output of depfun now includes replaceZeroWithNaN2.

MATLAB Compiler 然后应该包括 replaceZeroWithNaN2 并且您的可执行文件应该可以工作.

MATLAB Compiler should then include replaceZeroWithNaN2 and your executable should work.

您可能还想向 MathWorks 报告该问题:在我看来,依赖项分析确实应该已经开始使用 replaceZeroWithNaN2,这可能是一个错误.

You might also like to report the issue to MathWorks: it feels to me like the dependency analysis really should be picking up replaceZeroWithNaN2 already, and it's maybe a bug.

这篇关于matlab可执行文件中的匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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