Call a function with an external application without opening a new Matlab instance

Is there a way to call Matlab functions from outside, in particular Windows cmd(but also a Linux terminal, LUA scripts, etc.) WITHOUT opening a new Matlab instance every time?

for example in cmd:

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

opens a new instance of Matlab relatively quickly and performs my function. Opening and closing this abbreviated matlab prompt takes about 2 seconds (without calculations) - hence, for 4,000 executions over 2 hours. I would like to avoid this, since the called function is always in the same workspace. Can this always be done in the same instance?

I already did some research and found the MATLAB COM Automation Server feature , but for me it seems rather complicated, and I do not see the basic steps to make it work for my business. Any tips for this?

I am not familiar with c/c++/c#, but I am thinking about using python(but only in the worst case).

+4
source share
4 answers

Based on @Ilya Kobelevskiy's broken but well-thought out idea, here's the final solution:

 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 the executable file of the Matlab function:

mcc -m myfunction

run this .exefile with cmd:

cd myCurrentDirectory && myfunction.exe parameter1 parameter2

, , .m .

:

  • , Matlab , .
  • , , .
  • apporach, ,
+3

MATLAB -r , , , . , .

MATLAB C/++, MATLAB MATLAB engine, MATLAB.

MATLAB Automation Server, , . , , , .

, MATLAB undocumented , Java, , , , .


: R2014b, MATLAB MATLAB Engine Python, MATLAB Python script. R2016b MATLAB Engine Java. - Java-, , .

+1

, , , matlab. , matlab.

, myRand.m

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

matlab ,

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

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);, matlab

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

, 4000 .

+1

, Magla, , , , - Matlab.

, . , , , , , , script/function, matlab .

, , , ( , / ).

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;

, , .

+1

Source: https://habr.com/ru/post/1542904/