These are simple examples to see the integration of Python in MATLAB (and vice versa).
To run this project, you need:
- MATLAB
- Python
- MATLAB Compiler SDK for the example 4 Generate Python library
- Init the from_matlab_to_python_and_back submodule
Note that depending on your MATLAB version, this link lists to the supported Python versions.
- Call Python from MATLAB and back
- Pass MATLAB to Python and back
- Call Python class
- Generate Python library
This example shows how to call python code or file through 3 approaches
% Firstly, verify Python configuration
p = pyenv
% Call python with the *py* prefix
py.list({'Name1','Name2','Name3'})
% Run Python statements with [*pyrun*](https://www.mathworks.com/help/matlab/ref/pyrun.html) function
pyrun("a = b*c", b = 5, c = 10)
% Run Python file with [pyrunfile](https://www.mathworks.com/help/matlab/ref/pyrunfile.html) function
pyrunfile("hello.py")
This example shows how to pass data - structures, table, timetable - between MATLAB & Python.
strMATLAB = "text"
strPython = py.str(strMATLAB)
strPython =
Python str with no properties.
text
This example shows how to call and pass classes between MATLAB & Python.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
classdef Person
properties
name
age
end
methods
function obj = Person(name, age)
%UNTITLED3 Construct an instance of this class
% Detailed explanation goes here
obj.name = name;
obj.age = age;
end
end
end
This example shows how to generate a Python library from a MATLAB function.
Please note that MATLAB Compiler SDK is needed.
buildResults = compiler.build.pythonPackage