Use MATLAB Engine Workspace in Python
This example shows how to add variables to the MATLAB® engine workspace in Python®.
When you start the engine, it provides an interface to a collection of all
MATLAB variables. This collection, named
implemented as a Python dictionary that is attached to the engine. The name of each
MATLAB variable becomes a key in the
The keys in
workspace must be valid MATLAB identifiers (e.g., you cannot use numbers as keys). You can add
variables to the engine workspace in Python, and then you can use the variables in MATLAB functions.
Add a variable to the engine workspace.
import matlab.engine eng = matlab.engine.start_matlab() x = 4.0 eng.workspace['y'] = x a = eng.eval('sqrt(y)') print(a)
In this example,
x exists only as a Python variable. Its value is assigned to a new entry in the engine
y, creating a MATLAB variable. You can then call the MATLAB
eval function to execute the
statement in MATLAB and return the output value, 2.0, to Python.