从C#应用程序内部调用Matlab编译器会引发异常 [英] Calling Matlab Compiler from inside C# app throws exception
问题描述
我找到了这段代码来调用Matlab编译器,当从Matlab命令提示符处调用该函数时,它可以正常工作,我将此函数构建为.Net Assembly,但是每当我尝试在我的C#应用程序中使用它以构建一些代码时, .m文件我例外,您认为我的问题在哪里?
I found this code to invoke the Matlab compiler, it works fine when the function is called from Matlab command prompt, I build this function to .Net Assembly but whenever I try to use it in my C# app in order to build some .m file I get an exception, where do you think my problem is?
Matlab代码:
function compileCode(mfile,dllName , dnetdir)
%% Create directories if needed
if (exist(dnetdir, 'dir') ~= 7)
mkdir(dnetdir);
end
%% Build .NET Assembly
eval(['mcc -N -d ''' dnetdir ''' -W ''dotnet:' dllName ',' ...
'' dllName ',0.0,private'' -T link:lib ''' mfile '''']);
end
C#代码:
var cmm = new compiler.MatlabCompiler();
MWCharArray x = new MWCharArray(@"C:\Users\ePezhman\Documents\MATLAB\Graph2D.m");
MWCharArray y = new MWCharArray("Graph");
MWCharArray z = new MWCharArray(@"C:\Matlab\dotnet");
cmm.compileCode(x,y,z);
例外:
... MWMCR :: EvaluateFunction错误...的未定义函数'mcc' 类型为"char"的输入参数.第9行=> compileCode.m错误.
... MWMCR::EvaluateFunction error ... Undefined function 'mcc' for input arguments of type 'char'. Error in => compileCode.m at line 9.
... Matlab M代码堆栈跟踪... 在文件C:\ Users \ ePezhman \ AppData \ Local \ Temp \ ePezhman \ mcrCache8.0 \ compil0 \ compiler \ compileCode.m中, 名称compileCode,第9行.
... Matlab M-code Stack Trace ... at file C:\Users\ePezhman\AppData\Local\Temp\ePezhman\mcrCache8.0\compil0\compiler\compileCode.m, name compileCode, line 9.
推荐答案
有趣的是,我假设您正在尝试编译一个可以动态编译其他函数的函数.
Interesting, I assume you are trying to compile a function that can dynamically compile other functions..
不幸的是,我不认为mcc
函数本身可以编译/部署
Unfortunately, I dont think the mcc
function can be compiled/deployed itself
确切地说,您看到的问题是因为MATLAB需要知道在编译时调用的所有函数,并且通过使用eval
,它不会自行解决(因为它不会在字符串内部进行解析) .您可以通过为编译器编写特殊注释来解决此特定问题. /p>
To be exact, the problem you are seeing is because MATLAB needs to know all functions called at compile-time, and by using eval
, it wont figure it out on its own (since it wont parse inside the string). You can fix this particular issue by writing special comments for the compiler..
function myEval()
%#function foo
eval('...');
end
(另一种替代方法是使用功能句柄).
(Another alternative is using function handles).
即使执行此操作,它也会在运行时在mcc
函数内部失败,提示:"License checkout failed, [...] Cannot find a valid license for Compiler"
.
Still even if you do that, it will fail at runtime inside the mcc
function saying that: "License checkout failed, [...] Cannot find a valid license for Compiler"
.
原因如注释中所述,mcc
是开发工具,不能部署到仅依赖于免费MCR运行时的独立程序中.
The reason is as mentioned in the comments, mcc
is a development tool and cannot be deployed to standalone programs which only depends on the free MCR runtime.
考虑一下,如果可能的话,它将破坏购买产品许可证的全部目的,因为您可以创建一个独立的程序,该程序可以在没有编译器工具箱的情况下编译其他代码:)
Think about it, if it was possible, it would defeat the whole purpose of buying licenses for the product, as you could create a standalone program that can compiler other codes without having the Compiler toolbox :)
这篇关于从C#应用程序内部调用Matlab编译器会引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!