简体   繁体   English

Matlab可执行文件中的匿名函数

[英]anonymous function in matlab executable

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. 但是,当我编译它们时,matlab的表现就好像我不包含其中一个文件,即使该文件已包含在部署中一样。

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: 编译后,出现以下错误:

Undefined function 'replaceZeroWithNaN2' for input arguments of type 'double' 类型为“ double”的输入参数的未定义函数“ replaceZeroWithNaN2”

The following is from MATLAB's guide on code generation (from ver. 2011b) : 以下是MATLAB的代码生成指南(自2011b版起) 不支持的代码功能

Clearly your use of the anonymous function is causing issues. 显然,您对匿名函数的使用会引起问题。

It's possible that this has changed in the time since this guide was published, but I would be a bit surprised. 自从本指南发布以来,这种情况有可能发生了变化,但是我会感到有些惊讶。 Try reworking your code to avoid anonymous function calls; 尝试重新编写代码以避免匿名函数调用; you'll probably save a bit of overhead anyway. 无论如何,您可能会节省一些开销。

Edit: 编辑:

After a bit more digging I found here that MATLAB now supports objects, however, anonymous functions are still unsupported. 经过进一步的挖掘后, 我发现此处的 MATLAB现在支持对象,但是仍然不支持匿名函数。

When MATLAB Compiler packages your code into an executable, it needs to include all the files that your main function depends on. 当MATLAB Compiler将您的代码打包为可执行文件时,它需要包括主要功能依赖的所有文件。 It does this using dependency analysis, ie walking through the code to see which things it depends on, and which things those things depend on. 它使用依赖关系分析来执行此操作,即遍历代码以查看其依赖的内容以及这些依赖的内容。

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. 例如,如果您的代码调用了eval('myfunction') ,它将不会找到myfunction作为依赖项。

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. 无论出于何种原因,依赖性分析replaceZeroWithNaN2都没有找到replaceZeroWithNaN2 ,并且它没有包含在可执行文件中,因此您将看到错误。 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 . 您可以通过运行depfun('testMain.m')自己检查这一点depfun是MATLAB用于查找依赖项的命令,并且输出显示它正在查找对testClass的依赖项,而不是replaceZeroWithNaN2

In cases like this you can explicitly tell the dependency analysis to include a function. 在这种情况下,您可以显式告诉依赖项分析包括一个函数。

Put the following comment at the top of testClass.m : 将以下注释放在testClass.m的顶部:

%#function replaceZeroWithNaN2

The %#function is a pragma that explicitly tells the dependency analysis that the code below depends on the function replaceZeroWithNaN2 . %#function是一个replaceZeroWithNaN2 ,可明确告知依赖项分析以下代码取决于函数replaceZeroWithNaN2 When I do that, the output of depfun now includes replaceZeroWithNaN2 . 当我这样做时, depfun的输出现在包括replaceZeroWithNaN2

MATLAB Compiler should then include replaceZeroWithNaN2 and your executable should work. 然后,MATLAB编译器应包含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. 您可能还想将问题报告给MathWorks:在我看来,依赖分析确实应该已经开始使用replaceZeroWithNaN2 ,这可能是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM