5

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).

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • 1
    Here is [an example](http://stackoverflow.com/questions/12306333/matlab-command-from-bash-command-line-on-an-already-running-session/12307838#12307838) on SO that uses the terminal multiplexer [tmux](http://tmux.sourceforge.net/) to attach a process on an already running matlab session. – marsei Sep 13 '13 at 09:38
  • @Magla : seems to be the perfect solution for Linux, I'll defintely try it when I may port my interface to Linux. At the moment I'm limited to Windows and try to find an interim solution here. – Robert Seifert Sep 13 '13 at 09:59
  • another piece of software called [screen](http://www.gnu.org/software/screen/) that, contrary to tmux, can be executed via cygwin (see http://cygwin.com/ml/cygwin-announce/2013-06/msg00026.html) – marsei Sep 13 '13 at 10:08

4 Answers4

3

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
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
1

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.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
1

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.

marsei
  • 7,691
  • 3
  • 32
  • 41
  • the basic issue with your suggestion is that the parameters of e.g. the second function call are determined from the parameters of the first call. And the outputs and inputs between two calls are processed by another application. – Robert Seifert Sep 13 '13 at 12:54
1

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.

Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41
  • Great approach! I had to make some essential edits to make it work, 1) I couldn't use the `!isfile` command and 2) it is necessary to delete the inputfile after every iteration. Have a look at my edits, then I accept the answer. – Robert Seifert Sep 14 '13 at 12:58
  • the peer rewievers declined my edits, I don't know why. But like this your answer is not working. – Robert Seifert Sep 14 '13 at 13:29
  • @thewaywalk - Right, I didn't had matlab on my computer when writing answer, so couldn't check syntax - it was more to illustrate idea, not a working example. I'm glad that approach worked out! – Ilya Kobelevskiy Sep 16 '13 at 13:48