The MATLAB Engine API allows accessing MATLAB functionality from a C program. To set up a MATLAB Engine session, one calls the engOpen
function. By default, this launches a new instance of MATLAB, for the exclusive use of Engine applications.
What I want is to use an already running MATLAB session from an Engine application so I can access its variables. I need to do this on Linux and OS X.
The Engine interface has very different implementations on Windows (where it uses a COM server) and Unix. On Unix we have to pass the location of the MATLAB executable to engOpen
, while on Windows (where the Engine uses a COM server) we don't. On Windows it is possible to use an existing instance of MATLAB for Engine applications: just run enableservice('AutomationServer', true)
. Is there any solution on Unix systems?
I am hoping that there is a general solution—perhaps undocumented—because, based on the documentation, the Python interface seems to allow this. I tested this on OS X and it works. I can, for example, start up an interactive MATLAB session, set a variable a=5
, then start Python, connect to the same session and be able to retrieve this variable. It is also possible to connect to an already running session from Java.
I need to do this from C however, not from Python, as it will be used in MATLink, the Mathematica-MATLink interface. How does the Python interface achieve this? How can I reproduce the same using C?
If there is an alternative C API than the "MATLAB Engine" that makes this possible (perhaps some other documented or undocumented C API that the Python interface is based on), I can accept that as an answer. I want a C program that can make use of an already running interactive MATLAB session.
Update:
Some poking around in the Python interface reveals that it makes use of a library called libmwengine_api
. This is not the same as the documented MATLAB Engine C API. We can look at the symbols in this library. On OS X,
nm -g libmwengine_api.dylib | c++filt
Then we can google for these symbols or grep the MATLAB installation directory for files containing them. There is nothing in plain text that turns up.
Based on this I believe that the Python interface uses an undocumented C++ Engine API which is distinct from the old, documented C one.
libmwengine_api
library. Listing symbols in this library reveals that it is C++, not C. That is not a problem for me, I use C++ anyway.grep
ping the whole MATLAB installation directory for e.g.findSharedMATLAB
does not reveal any associated header files (only binaries), which strongly suggests that this interface is undocumented. – Token