4

I have some MATLAB script, for example:

function mat_foo(varargin)
    params.x = 'aaa';
    params = parse_input(params, varargin);
    disp(params.x);
end

parse_input is a function I have which convert data from varargin and override the defaults at 'params' struct.

I compiled this function and I want to call it from python, I do it the following way:

subprocess.check_call(['$/mat_foo.app/Contents/MacOS/applauncher x bbb'], shell=True)

This sets params.x to 'bbb' and works well.

My problem is that each time I want to call a compiled MATLAB it initializes the MCR and takes about 8-10 seconds. My question is if there is a way to initialize the MCR once and use it many times quickly? I use MATLAB R2013a and python 2.7.5 on OSX

3
  • I have seen compiled matlab code get called from php without 10 second waiting times at each call. So though I don't know how to achieve it, it must definitely be possible. Just to eliminate the improbable, could you test whether the problem also occurs with a trival function (that you can show here as a reproducible example). Commented Aug 21, 2014 at 11:44
  • @DennisJaheruddin: Maybe the code was generated using matlab coder, such code does not require MCR. Commented Aug 21, 2014 at 19:34
  • @Daniel The code was compiled by myself, so it definitely requires the MCR. I just don't know the details of how it is called but I think this can be considered proof that it can be done. Commented Aug 21, 2014 at 19:40

1 Answer 1

2

It is possible to compile your code in a shared library as described here. You can load this library in python with

mymatlab = cdll.LoadLibrary("mymatlab_library.so")

and initialize and load the MCR by calling a function

mymatlab.initializeMyLibrary()

that might do nothing or only prints a text to the console with matlab's disp function.

Subsequent function calls to your library should execute immediately.

See also this Mathworks discussion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.