This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Attach to Existing MATLAB Sessions

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

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.

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

Shut down any MATLAB sessions.

From the Start button on the Windows menu bar, click Run.

In MATLAB, capture the value returned by the command:

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

In the Run dialog box Open field, type the following command, replacing path with the value from the previous step:

path\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:


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

Related Topics