Call a function by an external application without opening a new instance of Matlab

天涯浪子 提交于 2019-12-17 06:16:25

问题


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?

for example in cmd:

matlab -sd myCurrentDirectory -r "function(parameters)" -nodesktop -nosplash -nojvm

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?

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?

I'm not familiar with c/c++/c# but I'm thinking about the use of python (but just in the worst case).


回答1:


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;

Another convenient solution would be to compile an executable of the Matlab function:

mcc -m myfunction

run this .exe-file using cmd:

cd myCurrentDirectory && myfunction.exe parameter1 parameter2

Be aware that the parameters are now passed as strings and the original .m-file needs to be adjusted considering that.

further remarks:

  • I guess Matlab still needs to be installed on the system, though it is not necessary to run it.
  • I don't know how far this method is limited respectively the complexity of the underlying function.
  • The speed-up compared to the initial apporach given in the question is relatively small



回答2:


Amongst the several methods exposed here, there is one workaround that should reduce the execution time of your multiple matlab calls. The idea is to run a custom function multiple times within on matlab session.

For example, myRand.m function is defined as

function r = myRand(a,b)
r = a + (b-a).*rand;

Within the matlab command window, we generate the single line command like this

S = [1:5; 1:5; 101:105];
cmd_str = sprintf('B(%d) = myRand(%d,%d);', S)

It generates the following command string B(1) = myRand(1,101);B(2) = myRand(2,102);B(3) = myRand(3,103);B(4) = myRand(4,104);B(5) = myRand(5,105); that is executed within a single matlab session with

matlab -nojvm -nodesktop -nosplash -r "copy_the_command_string_here";

One of the limitation is that you need to run your 4000 function calls in a row.




回答3:


I like approach proposed by Magla, but given the constrains stated in your comment to it, it can be improved to still run single function in one matlab session.

Idea is to pipe your inputs and outputs. For inputs, you can check if certain input file exists, if it does, read input for your function from it, do work, write output to another file to signal script/function processing results that it matlab function is done and is waiting for the next input.

It is very straightforwad to implement using disk files, with some effort it is probably possible to do through memory disk (i.e., open input/output fiels in RAM).

function pipeConnection(numIterations,inputFile,outputFile)

for i=1:numIterations

while(!isfile(inputFile))
sleep(50);
end;

% Read inputs

output = YourFunction(x,y,z);

% Write output to file, go to next iteration

end;
return;

If number of iterations is unknown when you start, you can also encode exit conditions in input file rather than specifying number of iterations right away.




回答4:


If you're starting up MATLAB from the command line with the -r option in the way you describe, then it will always start a new instance as you describe. I don't believe there's a way around this.

If you are calling MATLAB from a C/C++ application, MATLAB provides the MATLAB engine interface, which would connect to any running instance of MATLAB.

Otherwise the MATLAB Automation Server interface that you mention is the right way to go. If you're finding it complicated, I would suggest posting a separate question detailing what you've tried and what difficulties you're having.

For completeness, I'll mention that MATLAB also has an undocumented interface that can be called directly from Java - however, as it's undocumented it's very difficult to get right, and is subject to change across versions so you shouldn't rely on it.


Edit: As of R2014b, MATLAB makes available the MATLAB Engine for Python, via which you can automate MATLAB from a Python script. And as of R2016b, there is also the MATLAB Engine for Java. If anyone was previously considering the undocumented Java techniques mentioned above, this would now be the way to go.



来源:https://stackoverflow.com/questions/18781803/call-a-function-by-an-external-application-without-opening-a-new-instance-of-mat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!