You are on page 1of 1

Using MATLAB® and Python® Together

The ≥ icon provides links to relevant sections of the MATLAB documentation to


learn more.

Call Python in MATLAB Call MATLAB in Python Create Python Package


Access settings and status of Python interpreter: Install MATLAB Engine API for Python Package MATLAB functions
>> pe = pyenv Run setup.py from an OS command window Use the Library Compiler App to
$ cd [matlabroot]/extern/engines/ create a Python package for
Specify version to use: python MATLAB functions
>> pe = pyenv("Version",'3.7') $ python setup.py install

Call Python modules and functions: Call MATLAB functions


py.module _ name.function _ name Import the module and start the engine
>>> import matlab.engine
>> py.math.sqrt(42) >>> eng =
matlab.engine.start _ matlab()
Pass keyword arguments
Use pyargs to pass keyword arguments Call functions through the engine Invoke MATLAB functions from the
>>> foo(5,bar=42) >>> x = eng.sqrt(42.0) Python package
>> py.foo(5,pyargs('bar',42)) >>> import PackageName
Capture multiple outputs >>> pkg =
Reload modules >>> x = eng.gcd(42.0,8.0,nargout=3) PackageName.initialize()
Reload the module after making updates: >>> result = pkg.foo()
>> py.importlib.reload(module) Stop the engine
>>> eng.exit() Close package
>>> pkg.terminate()

Data Type Conversions Data Science Libraries


Data types will be automatically Some MATLAB data types need to be Apache Parquet
converted where possible. converted.

MATLAB Python MATLAB Conversion Function


double, single float categorical char
complex single complex string char
complex double table table2struct
Use Apache Parquet to efficiently transfer tabular
(u)int8, (u)int16, int data
timetable timetable2struct
(u)int32,(u)int64 From MATLAB:
datetime char >> tbl = parquetread(fname)
NaN float(nan)
Note: The default numeric type is integer in >> parquetwrite(tbl,fname)
Inf float(inf)
Python and double in MATLAB when typing 42
String, char str From Python:
Logical bool >>> df = pandas.read _ parquet(fname)
To create a float in Python:
Structure dict >>> x = 42.0 >>> pandas.Dataframe.to _ parquet(df)
Vectors array.array() >>> x = float(42)
Deep Learning
Cell array list, tuple
Create integer from MATLAB: Access frameworks in MATLAB with importers for
>> x = int32(42) TensorFlow-Keras, ONNX, etc.
>> net = importKerasNetwork(model)

mathworks.com

© 2022 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks. 2/22
Other product or brand names may be trademarks or registered trademarks of their respective holders.

You might also like