0% found this document useful (0 votes)
6 views1 page

Calling Python From Matlab Cheat Sheet

This document provides guidance on calling Python from MATLAB, including setup requirements, running Python code, and data type conversions. It outlines how to configure the Python environment, execute scripts, and integrate Python with Simulink. Additionally, it covers the use of data science libraries and deep learning model access within MATLAB.

Uploaded by

x2prli
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Calling Python From Matlab Cheat Sheet

This document provides guidance on calling Python from MATLAB, including setup requirements, running Python code, and data type conversions. It outlines how to configure the Python environment, execute scripts, and integrate Python with Simulink. Additionally, it covers the use of data science libraries and deep learning model access within MATLAB.

Uploaded by

x2prli
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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​
[Link]
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: >> [Link] _
>> pe = pyenv [Link] _ name
>> x = [Link](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 >> [Link](5,bar=42)
view its value in MATLAB:
To execute a few lines of code: >> [Link](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:
>> [Link](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. >> [Link]();
(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 = [Link] _ parquet(fname)
Dictionary dict
>>> [Link] _ parquet(df) >u fon
y
> > System >
Struct dict
MATLAB Function
Table [Link]. Deep Learning
dataframe
Datetime [Link]. Access models in MATLAB with importers for
datetime TensorFlow™, PyTorch®, and ONNX™. Implement Python modules in Simulink using
Duration [Link]. MATLAB Function block or MATLAB System
timedelta
>> net = importKerasNetwork(model) block.
Note: The default numeric type is integer in
Python and double in MATLAB.

[Link]

© 2025 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See [Link]/trademarks for a list of additional trademarks.
Other product or brand names may be trademarks or registered trademarks of their respective holders.

You might also like