Running compiled MATLAB from python
I have a 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 that changes data from varargin
and overrides the default values in 'params'
struct.
I have compiled this function and I want to call it from python, I do it like this:
subprocess.check_call(['$/mat_foo.app/Contents/MacOS/applauncher x bbb'], shell=True)
It installs params.x
in 'bbb'
and works well.
My problem is that every time I want to call the compiled MATLAB it initializes the MCR and takes about 8-10 seconds. My question is, is there a way to initialize MCR once and use it many times faster? I am using MATLAB R2013a and python 2.7.5 on OSX
source to share
You can compile your code into a shared library, as described here . You can load this library in python with
mymatlab = cdll.LoadLibrary("mymatlab_library.so")
and initialize and load MCR by calling the function
mymatlab.initializeMyLibrary()
which can't do anything or just prints text to the console using matlab function disp
.
Subsequent function calls to your library should be made immediately.
See also Discussion Mathworks .
source to share