Main Content

Attach to ExistingMATLABSessions

This example shows how to attach an engine program to a MATLAB®session that is already running.

On a Windows®platform, start MATLAB with-automationin the command line. When you callengOpen, it connects to this existing session. CallengOpenonly once, because anyengOpencalls now connect to this one MATLAB session.

The-automation选项也会引起command window to be minimized. You must open it manually.

On themacOSand Linux®platforms, you cannot make an engine program connect to an existing MATLAB session.

Shut down any MATLAB sessions.

From theStartbutton on the Windows menu bar, clickRun.

In MATLAB, capture the value returned by the command:

path = fullfile(matlabroot,'bin',computer('arch'))

In theRundialog boxOpenfield, type the following command, replacingpathwith the value from the previous step:

path\matlab.exe -automation

To start MATLAB, clickOK.

In MATLAB, copy theengwindemo.cexample to a writable folder.

copyfile(fullfile(matlabroot,'extern','examples','eng_mat','engwindemo.c'),'.','f')

Build the example.

mex-clientengineengwindemo.c

Run theengwindemoprogram by typing at the MATLAB prompt:

!engwindemo

This command does not start another MATLAB session, but rather uses the MATLAB session that is already open.