Attach to Existing MATLAB Sessions
This example shows how to attach an engine program to a MATLAB® session on a Windows® platform that is already running.
MATLAB does not support connecting an engine program to an existing MATLAB session on the macOS and Linux® platforms.
On a Windows platform, start MATLAB with -automation in the command line. When you call
engOpen, it connects to this existing session. Call
engOpen only once, because any engOpen calls now
connect to this one MATLAB session.
The -automation option also causes the command window to be
minimized. You must open it manually.
In MATLAB, capture the value returned by the command:
mlpath = fullfile(matlabroot,"bin","win64")
Shut down all MATLAB sessions.
From the Start button on the Windows menu bar, type run.
In the Run dialog box Open
field, type the following command, replacing mlpath with the
value from the path command:
mlpath\matlab.exe -automation
To start MATLAB, click OK.
In MATLAB, copy the engwindemo.c example to a writable
folder.
copyfile(fullfile(matlabroot,"extern","examples","eng_mat","engwindemo.c"),".", "f")
Build the example.
mex -client engine engwindemo.c
Run the engwindemo program by typing at the MATLAB prompt:
!engwindemoThis command does not start another MATLAB session, but uses the MATLAB session that is already open.