在不打开新的 Matlab 实例的情况下,由外部应用程序调用函数 [英] Call a function by an external application without opening a new instance of Matlab
问题描述
有没有办法从外部调用 Matlab 函数,特别是通过 Windows cmd
(还有 Linux 终端、LUA 脚本等),而无需打开一个新的实例Matlab 每次?
Is there a way to call Matlab functions from outside, in particular by the Windows cmd
(but also the Linux terminal, LUA-scripts, etc...), WITHOUT opening a new instance of Matlab each time?
例如在cmd
中:
matlab -sd myCurrentDirectory -r "function(parameters)" -nodesktop -nosplash -nojvm
相对较快地打开一个新的 Matlab 实例并执行我的函数.打开和关闭这个简化的 matlab 提示大约需要 2 秒(没有计算)——因此 4000 次执行需要超过 2 小时.我想避免这种情况,因为被调用的函数总是位于同一个工作区中.可以一直在同一个实例中完成吗?
opens a new instance of Matlab relatively fast and executes my function. Opening and closing of this reduced matlab prompt takes about 2 seconds (without computations) - hence for 4000 executions more than 2 hours. I'd like to avoid this, as the called function is always located in the same workspace. Can it be done in the same instance always?
我已经做了一些研究,发现了 MATLAB COM 自动化服务器,但对我来说似乎很复杂,我没有看到使其适用于我的案例的基本步骤.对此有什么建议吗?
I already did some research and found the possibility of the MATLAB COM Automation Server, but it seems quite complicated to me and I don't see the essential steps to make it work for my case. Any advices for that?
我不熟悉 c/c++/c#
但我正在考虑使用 python
(但只是在最坏的情况下).
I'm not familiar with c/c++/c#
but I'm thinking about the use of python
(but just in the worst case).
推荐答案
基于@Ilya Kobelevskiy 提出的不可行但经过深思熟虑的最终解决方案:
Based on the not-working, but well thought, idea of @Ilya Kobelevskiy here the final workaround:
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;
另一个方便的解决方案是编译 Matlab 函数的可执行文件:
Another convenient solution would be to compile an executable of the Matlab function:
mcc -m myfunction
使用cmd
运行这个.exe
文件:
cd myCurrentDirectory && myfunction.exe parameter1 parameter2
请注意,参数现在作为字符串传递,原始.m
文件需要考虑到这一点进行调整.
Be aware that the parameters are now passed as strings and the original .m
-file needs to be adjusted considering that.
补充说明:
- 我猜仍然需要在系统上安装 Matlab,不过没有必要运行它.
- 我不知道这种方法在多大程度上限制了底层功能.
- 与问题中给出的初始方法相比的加速是相对较小
这篇关于在不打开新的 Matlab 实例的情况下,由外部应用程序调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!