通过外部应用程序调用函数,而无需打开Matlab的新实例

the*_*alk 5 matlab lua cmd batch-file function-calls

有没有一种方法可以从外部调用Matlab函数,尤其是通过Windows cmd(还可以通过Linux终端,LUA脚本等)调用Matlab函数,而不必每次都打开Matlab的新实例?

例如cmd

matlab -sd myCurrentDirectory -r "function(parameters)" -nodesktop -nosplash -nojvm
Run Code Online (Sandbox Code Playgroud)

相对较快地打开Matlab的新实例并执行我的功能。打开和关闭此简化的matlab提示大约需要2秒钟(无需计算),因此对于4000次执行而言,需要2个小时以上。我想避免这种情况,因为被调用的函数始终位于同一工作空间中。可以始终在同一实例中完成吗?

我已经进行了一些研究,找到了MATLAB COM Automation Server的可能性,但是对我来说似乎很复杂,并且我看不到使它适合我的情况的必要步骤。有什么建议吗?

我不熟悉,c/c++/c#但我正在考虑使用python(但在最坏的情况下)。

the*_*alk 3

基于 @Ilya Kobelevskiy 的不起作用但经过深思熟虑的想法,这里是最终的解决方法:

 function pipeConnection(numIterations,inputFile)

 for i=1:numIterations

 while(exist('inputfile','file'))

     load inputfile;
     % read inputfile -> inputdata
     output = myFunction(inputdata);

     delete('inputfile');
 end

 % Write output to file
 % Call external application to process output data
 % generate new inputfile 

 end;
Run Code Online (Sandbox Code Playgroud)

另一个方便的解决方案是编译 Matlab 函数的可执行文件:

mcc -m myfunction
Run Code Online (Sandbox Code Playgroud)

.exe使用以下命令运行此文件cmd

cd myCurrentDirectory && myfunction.exe parameter1 parameter2
Run Code Online (Sandbox Code Playgroud)

请注意,参数现在作为字符串传递,并且.m考虑到这一点,需要调整原始文件。

进一步说明:

  • 我猜Matlab仍然需要安装在系统上,尽管没有必要运行它。
  • 我不知道这种方法对底层函数的复杂性的限制有多大。
  • 与问题中给出的初始方法相比,加速相对较小