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

后端 未结 4 1352
说谎
说谎 2020-11-28 15:43

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

相关标签:
4条回答
  • 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.

    0 讨论(0)
  • 2020-11-28 16:02

    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.

    0 讨论(0)
  • 2020-11-28 16:12

    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.

    0 讨论(0)
  • 2020-11-28 16:13

    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
    0 讨论(0)
提交回复
热议问题