Main Content

Initialize MATLAB Runtime

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

  1. Call the initialize_runtime() function, which allows you to provide a list of startup options to MATLAB Runtime. This loads and starts MATLAB Runtime.

  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.

Provide MATLAB Runtime Startup Options

Note

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.

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:

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:

import addmatrix

myAdder = addmatrix.initialize()

Note

If the initialize_runtime() function is not called before a call to initialize() function, MATLAB Runtime is started with no startup options.

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