Matlab可执行文件中的匿名函数 [英] anonymous function in matlab executable
问题描述
在编译它们之前,我有一个可以完美运行的三个文件.但是,当我编译它们时,即使已包含在部署中,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:
类型为'double'的输入参数的未定义函数'replaceZeroWithNaN2'
Undefined function 'replaceZeroWithNaN2' for input arguments of type 'double'
推荐答案
当MATLAB Compiler将您的代码打包到可执行文件中时,它需要包括主要功能依赖的所有文件.它使用依赖性分析来完成此操作,即遍历代码以查看其依赖于哪些事物,以及这些事物依赖于哪些事物.
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编译器应包含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屋!