Connect to already running MATLAB with MATLAB Engine
<p><a href="https://www.mathworks.com/help/matlab/matlab_external/introducing-matlab-engine.html" rel="nofollow noreferrer">The MATLAB Engine API</a> allows accessing MATLAB functionality from a C program.  To set up a MATLAB Engine session, one calls the <a href="https://www.mathworks.com/help/matlab/apiref/engopen.html" rel="nofollow noreferrer"><code>engOpen</code></a> function.  By default, this launches a new instance of MATLAB, for the exclusive use of Engine applications.</p>

<p><strong>What I want is to use an <em>already running</em> MATLAB session from an Engine application so I can access its variables.</strong>  I need to do this on Linux and OS X.</p>

<p>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 <code>engOpen</code>, while on Windows (where the Engine uses a COM server) we don't.  On Windows it <em>is</em> possible to use an existing instance of MATLAB for Engine applications: just run <a href="https://www.mathworks.com/help/matlab/ref/enableservice.html" rel="nofollow noreferrer"><code>enableservice('AutomationServer', true)</code></a>.  Is there any solution on Unix systems?</p>

<p>I am hoping that there is a general solution—perhaps undocumented—because, <a href="https://www.mathworks.com/help/matlab/matlab_external/connect-python-to-running-matlab-session.html" rel="nofollow noreferrer">based on the documentation, the Python interface seems to allow this</a>.  I tested this on OS X and it works.  I can, for example, start up an interactive MATLAB session, set a variable <code>a=5</code>, then start Python, connect <em>to the same session</em> and be able to retrieve this variable.  <a href="https://www.mathworks.com/help/matlab/matlab_external/connect-to-existing-shared-session.html" rel="nofollow noreferrer">It is also possible to connect to an already running session from Java.</a></p>

<p>I need to do this from C however, not from Python, as it will be used in <a href="http://matlink.org/" rel="nofollow noreferrer">MATLink</a>, the Mathematica-MATLink interface.  How does the Python interface achieve this?  How can I reproduce the same using C?</p>

<p>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 <em>interactive</em> MATLAB session.</p>

<hr>

<p><strong>Update:</strong></p>

<p>Some poking around in the Python interface reveals that it makes use of a library called <code>libmwengine_api</code>. This is not the same as the documented MATLAB Engine C API.  We can look at the symbols in this library. On OS X,</p>

<pre><code>nm -g libmwengine_api.dylib | c++filt
</code></pre>

<p>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.</p>

<p>Based on this I believe that the Python interface uses an undocumented C++ Engine API which is distinct from the old, documented C one. </p>