Integrate Python Code into Simulink Using Python Code Block
This topic shows how you can integrate your existing native Python® code into Simulink® using the Python Code block. Use this block to import or write native Python code directly into Simulink using the block dialog box. You can use the block dialog box to interface with Python code by defining block input ports, output ports, parameters, and persistent variables. The Python Code block enables you to integrate your existing native Python code into Simulink without having to refactor the code into existing Simulink or MATLAB® frameworks.
Configure System to Use Python
To execute Python code in Simulink, you must have a compatible version of Python installed. This might involve installing the corresponding Python development headers in Linux and Mac. For more information on supported Python versions and setting up your system to use Python in MATLAB and Simulink, see Configure Your System to Use Python.
You can access all standard Python libraries, third-party functionality, or user-created modules. For more information on using Python in MATLAB and Simulink, see Access Python Modules from MATLAB - Getting Started.
You must set the ExecutionMode
of Python in MATLAB as
InProcess
. The Python Code block does not support Python environment in
out-of-process execution mode. If you are using Linux or Mac, you must install the
corresponding Python development headers for your MATLAB Python version.
Use Python Code Block in Simulink
This cuboid_class.py
file defines a Cuboid
class
with methods to calculate its volume and surface area.
class Cuboid:
def setParameters(self, length, height, breadth):
self.length = length
self.height = height
self.breadth = breadth
def calculateVolume(self):
return (self.length*
self.breadth*
self.height)
def calculateSurfArea(self):
return 2*(self.length*self.breadth +
self.breadth*self.height +
self.height*self.length)
This section shows the steps to integrate this code into Simulink using the Python Code block. The block algorithm implements these conditions:
Block code instantiates a cuboid object and stores it as a persistent variable.
Length and breadth values of the cuboid object are configured as block inputs.
Height of the cuboid object is configured as a block parameter.
Each time step calculates and outputs the volume and surface area of the cuboid.
To integrate this code into Simulink, open a Simulink model and add the Python Code block. In the Simulink Library Browser, find the block under Simulink > User-Defined Functions > Python Code.
Double click the block to open the block dialog box which has these sections:
Code: Use this section to import and specify your Python code for the block. It contains three code tabs - Initialize, Output, and Terminate.
On the Initialize tab, write the one-time setup tasks that the block should perform before the start of simulation. On the Output tab, specify the Python code to define the block output for each time-step. In the Terminate pane, specify the tasks that the block should execute before the simulation concludes.
Ports and Parameters: This section enables you to specify the block inputs, outputs, parameters, and persistent objects. Click Add to add one of these element to the block. Specify these properties for each element:
Name: Specify the name of the element as it will be used in the Python code.
Scope: Set the scope of the element. You can set it as Input, Output, Parameter, and Persistent. A Python Code block configured with persistent variable does not support continuous sample time and stepping back of simulation.
Label: Specify the name of the element as it appears in the block port or dialog box. This property does not apply for elements with persistent scope.
Type: Set the data type of the element. You can specify data types supported by Simulink, user-defined bus types, or PythonObject type to store Python objects. For more information in Simulink data types, see Data Types Supported by Simulink.
Dimension: Specify the size of the element.
Port: Specify the port number for this element. This property does not apply for elements with persistent scope.
Use the Ports and Parameters table to define the block interface. Click the Add button to add a new element to the table. Specify the name, scope, label, type, dimensions, and port number if applicable for the element.
For the cuboid code, add these elements:
Input port for length.
Input port for breadth.
Block parameter for height.
Persistent variable of type
PythonObject
for the cuboid object.Output port for volume.
Output port for surface area.
Define the block algorithm using the Initialize, Output, and Terminate panes in the code section of the block dialog box.
For the cuboid code, use the Initialize tab to import the class definition and initialize the cuboid object. You must add your user-defined python modules to the python search path. For for information, see Call User-Defined Python Module.
On the Output tab, set the object parameters, calculate volume, and calculate surface area. Assign the output of these methods to the specified output elements.
You can use the Python
print()
function in the block code in these code panes to display text in the Simulink diagnostic viewer in normal and accelerator mode simulation. This is not supported for rapid accelerator mode simulation.Double-click the block to open the block mask with the parameter field. Specify the parameter for the block.
The Python Code block is now configured with the required ports, parameters, and block algorithm. Observe the updated block diagram with specified block ports. You can now connect the ports as desired and simulate the model.