© 2020 The MathWorks, Inc. Using MATLAB with Python Twitter: @HeatherGorr Instagram: @heather.codes Heather Gorr, PhD Senior Product Manager, MATLAB MathWorks
© 2020 The MathWorks, Inc.
Using MATLAB with Python
Twitter: @HeatherGorr
Instagram: @heather.codes
Heather Gorr, PhD
Senior Product Manager, MATLAB
MathWorks
Amazing applications are built using MATLAB!
https://www.mathworks.com/company/user_stories.html
.NET
Java
C/ C++
Python HTTP
https://www.mathworks.com/help/matlab/external-language-interfaces.html
MATLAB provides flexible integration with multiple languages
Using MATLAB with Python
▪ Example overview
▪ Calling Python libraries from MATLAB
▪ Calling MATLAB from Python
– via MATLAB Engine API
– via MATLAB Runtime (MATLAB Compiler SDK)
– via MATLAB Production Server
▪ Additional info
– Data management
– Deep learning
– Troubleshooting
▪ Resources
Sentiment Analysis Example
Example files:
https://github.com/mathworks/matlab-with-python
“That movie
was terrible!”
Sentiment Analysis Example
Sentiment Analysis Example
MATLABPython
Sentiment Analysis Example: Calling Python from MATLAB
MATLAB
Python
Sentiment Analysis Example: Calling MATLAB from Python
MATLAB
Python
Using MATLAB with Python
▪ Calling libraries written in Python from MATLAB– MATLAB
▪ Calling MATLAB from Python– MATLAB
▪ Packaging MATLAB programs for
scalable deployment with Python
– MATLAB Compiler SDK
– MATLAB Production Server
MATLAB Type Example Python Type Example
Scalar double 9.0 Float 9.0
Char 'A' Str 'A'
Cell array {'apple',2,1} List ['apple',2,1]
1-by-n double [1.0, 2.0, 3.0] array.array(‘d’) [1.0, 2.0, 3.0]
m-by-n matrix [ 1.0, 2.0
3.0, 4.0 ]
MATLAB Type Example Python Type Example
Scalar double 9.0 Float 9.0
Char 'A' Str 'A'
Cell array {'apple',2,1} List ['apple',2,1]
1-by-n double [1.0, 2.0, 3.0] array.array(‘d’) [1.0, 2.0, 3.0]
m-by-n matrix [ 1.0, 2.0
3.0, 4.0 ]memoryview object
MATLAB Type Example Python Type Example
Scalar double 9.0 Float 9.0
Char 'A' Str 'A'
Cell array {'apple',2,1} List ['apple',2,1]
1-by-n double [1.0, 2.0, 3.0] array.array(‘d’) [1.0, 2.0, 3.0]
MATLAB Type Example Python Type Example
Scalar double 9.0 Float 9.0
MATLAB Type Example Python Type Example
Scalar double 9.0 Float 9.0
Char 'A' Str 'A'
MATLAB Type Example Python Type Example
Scalar double 9.0 Float 9.0
Char 'A' Str 'A'
Cell array {'apple',2,1} Tuple ['apple',2,1]
MATLAB Converts Core Python Types
>> x = 9.0;
>> a = py.math.sqrt(x);
>> class(a)
'double'
https://www.mathworks.com/help/matlab/python-data-types.html
>> py.print('hello','world',...
pyargs('sep',', '))
>> py.math.sqrt(42)
MATLAB
>>> import math
>>> math.sqrt(42)
Python
>>> print('hello','world',sep=', ')
Note the syntax differences when calling Python from MATLAB
>> [s,sidx] = sort(x)
MATLAB
>>> s = eng.sort(x,nargout=2)
Python
Note the syntax differences when calling MATLAB from Python
>> C = A + B >>> C = eng.plus(A,B)
>> foo(x) >>> eng.foo(x,nargout=0)
MATLAB Production Server manages multiple MATLAB Runtime
versions simultaneously
▪ RESTful API:
– https://www.mathworks.com/help/mps/restful-api-and-json.html
▪ Python Client:
– https://www.mathworks.com/help/mps/python/create-a-matlab-production-server-python-
client.html
Using MATLAB with Python for Deep Learning
▪ Import and export networks directly using ONNX and Tensorflow-Keras
importers:
▪ https://www.mathworks.com/solutions/deep-learning/models.html
Use Apache Parquet to store and transfer tabular data between
MATLAB and Python
▪ Working with Parquet files:
▪ https://www.mathworks.com/help/matlab/parquet-files.html
▪ MATLAB library for Apache Arrow on GitHub:
▪ https://github.com/apache/arrow/tree/master/matlab
Troubleshooting
▪ Sophisticated exception handling
options exist
▪ See the documentation
▪ Call tech support!
▪ MATLAB Answers, community
https://www.mathworks.com/help/matlab/getting-started-with-python.html
https://www.mathworks.com/help/matlab/matlab-engine-for-python.html
Resources
▪ General:
▪ https://www.mathworks.com/products/matlab/matlab-and-python.html
▪ Python from MATLAB:
▪ https://www.mathworks.com/help/matlab/call-python-libraries.html
▪ MATLAB from Python:
– MATLAB Engine API:
– https://www.mathworks.com/help/matlab/matlab-engine-for-python.html
– MATLAB Compiler SDK:
– https://www.mathworks.com/help/compiler_sdk/python_packages.html
– Data type conversions:
– https://www.mathworks.com/help/matlab/python-data-types.html
▪ Example:
▪ https://github.com/mathworks/matlab-with-python
Using MATLAB with Python
Python
MATLAB
+
@HeatherGorr
@heather.codes
Heather Gorr, PhD