Calling Python from MATLAB
This reference shows common use cases but is by no means comprehensive.
The >> icon provides links to relevant sections of the MATLAB® documentation.
For general information, see
https://www.mathworks.com/products/matlab/matlab-and-python.html.
Setup Run Python Code in MATLAB
Requirements Live Editor Task Call Python Script
To call Python® from MATLAB, you need to The Run Python Code task lets you To execute code organized in a script:
have both installed on your system. interactively run Python code or files, then >> outvars =
generate the code. pyrunfile(file,outputs)
Configuration
Access settings and the status of the Python Call Python Modules and Functions
interpreter: >> py.module _
>> pe = pyenv name.function _ name
>> x = py.math.sqrt(42)
Specify which version to use:
>> pe = pyenv("Version","3.9") Pass Keyword Arguments
Ensure the PYTHONHOME environment vari- Either call directly or use pyargs:
able aligns with your Python version. To >>> foo(5,bar=42)
Call Python Statements >> py.foo(5,bar=42)
view its value in MATLAB:
To execute a few lines of code: >> py.foo(5,pyargs('bar',42))
>> getenv("PYTHONHOME") >> outvars = pyrun(code,outputs)
Ensure the appropriate versions are on the >> x = pyrun("a = b*c","a", b = Reload Modules
system path: 5, c = 10) Reload the module after making updates:
>> py.importlib.reload(module)
>> getenv("PATH")
Data Type Conversions Data Science Libraries Integrate Python and Simulink
Data types will be automatically Apache Parquet Python Importer
converted where possible. You can use the Python Importer to import
MATLAB Python Python modules and packages to Simulink®.
Double, single Float Open the importer:
complex single Complex >> obj = Simulink.
Complex double PythonImporter();
(u)int8, (u)int16, int Use Apache ® Parquet to efficiently transfer data. >> obj.view();
(u)int32,(u)int64 From MATLAB:
NaN Float(nan) >> tbl = parquetread(fname) MATLAB Function Block and MATLAB
Inf Float(inf) >> parquetwrite(tbl,fname) System Block
String, char Str
Logical Bool From Python:
>>> df = pandas.read _ parquet(fname)
Dictionary dict
>>> pandas.Dataframe.to _ parquet(df) >u fon
y
> > System >
Struct dict
MATLAB Function
Table Py.pandas. Deep Learning
dataframe
Datetime Py.datetime. Access models in MATLAB with importers for
datetime TensorFlow™, PyTorch®, and ONNX™. Implement Python modules in Simulink using
Duration Py.datetime. MATLAB Function block or MATLAB System
timedelta
>> net = importKerasNetwork(model) block.
Note: The default numeric type is integer in
Python and double in MATLAB.
mathworks.com
© 2025 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks.
Other product or brand names may be trademarks or registered trademarks of their respective holders.