Main Content

Initialize MATLAB Runtime

When you integrate compiled MATLAB® functions into a Python® application, your code must initialize MATLAB Runtime and any compiled packages in the application.

  1. To initialize MATLAB Runtime with the ability to select startup options, call the initialize_runtime() function. This loads and starts MATLAB Runtime.

    Calling initialize_runtime() is optional. If you call initialize() without calling initialize_runtime() first, MATLAB Runtime is started with no startup options.

  2. Use the initialize() function of each compiled package in the application to retrieve a handle that can be used to call the MATLAB functions within the package. Call the initialize() function only once for each compiled package.

Provide MATLAB Runtime Startup Options

MacOS

On macOS, you must pass the MATLAB Runtime options to the mwpython command when starting Python. Use -mlstartup followed by a comma-separated list of MATLAB Runtime options. MATLAB Runtime options passed to initialize_runtime() are ignored.

All Platforms

The MATLAB Runtime has two startup options that you can specify:

  • -nojvm — disable the Java® Virtual Machine, which is enabled by default. This can help improve MATLAB Runtime's performance.

  • -nodisplay — on Linux®, run MATLAB Runtime without display functionality.

You specify these options before you initialize the compiled MATLAB functions. You do so by calling the initialize_runtime() method of a generated Python package with the MATLAB Runtime options. The list of MATLAB Runtime options is passed as a list of strings. For example, to start MATLAB Runtime for the package addmatrix with no display and no Java Virtual machine, use this code.

import addmatrix

addmatrix.initialize_runtime(['-nojvm', '-nodisplay'])

If your application uses multiple Python packages, you call initialize_runtime() from only one package. The first call sets the run-time options for MATLAB Runtime session. Any subsequent calls are ignored.

Start MATLAB Runtime with Compiled MATLAB Functions

To invoke a compiled MATLAB function, load it into MATLAB Runtime. Do this by calling the initialize() method of the generated Python package. The initialize() method returns an object that can be used to invoke the compiled MATLAB functions in the package. For example, to start MATLAB Runtime and load the MATLAB functions in the addmatrix package, use this code.

import addmatrix

myAdder = addmatrix.initialize()

Note

You cannot import matlab.engine after importing your component. For more information on matlab.engine, see Start and Stop MATLAB Engine for Python.

Related Topics