https://github.com/mathworks/call-simulink-from-python
Examples to show the two options to simulate a Simulink model from Python
https://github.com/mathworks/call-simulink-from-python
Last synced: about 2 months ago
JSON representation
Examples to show the two options to simulate a Simulink model from Python
- Host: GitHub
- URL: https://github.com/mathworks/call-simulink-from-python
- Owner: mathworks
- License: other
- Created: 2023-03-13T16:45:08.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-17T21:03:42.000Z (over 2 years ago)
- Last Synced: 2025-05-06T00:53:32.871Z (about 2 months ago)
- Language: MATLAB
- Size: 817 KB
- Stars: 23
- Watchers: 5
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Simulate a Simulink® model from Python®
This example illustrates two ways to simulate a Simulink model (named
the_model
) via a wrapper MATLAB® function (namedsim_the_model
) from Python.
The first approach, shown incall_sim_the_model_using_matlab_engine.py
, uses the [MATLAB Engine API for
Python](https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html) to call the wrapper functionsim_the_model.m
multiple times passing in different parameters and external input signals.
The second approach uses [MATLAB Compiler
SDK](https://www.mathworks.com/help/compiler_sdk/gs/create-a-python-application-with-matlab-code.html)™ and [Simulink Compiler](https://www.mathworks.com/help/slcompiler/ug/deploy-from-matlab-command-line.html)™ to first build a Python package around the wrapper functionsim_the_model.m
as shown inbuild_python_package_around_sim_the_model.m
. We can then call this package to run the warpper functionsim_the_model.m
multiple times passing in different parameters and external input signals as show incall_sim_the_model_using_matlab_runtime.py
.
This example includes the following files:
*
the_model.slx
: the Simulink model we will simulate in the example;
*sim_the_model.m
: the wrapper MATLAB function to simulate a Simulink model with the specified parameter and input signal values;
*call_sim_the_model.m
: MATLAB script used to call the sim_them_model multiple times in MATLAB with different inputs and parameters;
*plot_results.m
: MATLAB script used by call_sim_the_model to plot the results;
*call_sim_the_model_using_matlab_runtime.py
: Python script to call sim_the_model packaged function multiple times and plot the results;
*call_sim_the_model_using_matlab_engine.py
: Python script that uses MATLAB Engine API to call sim_the_model.m multiple time and plot the results;
*CallingSimFromPython.pptx
: complementary presentation slides describing the demo structure and setupThe model
the_model.slx
and the wrapper MATLAB functionsim_the_model.m
illustrate implementation choices that make data marshaling between Python andsim
command in MATLAB relatively straight forward and can be used with any Simulink model. These are:* Parameterizing the Simulink model using workspace variables makes it easy run sim with new parameter values passed in from Python.
* Labeling the logged signals in the model with valid identifiers, makes it easy to pack the results into a MATLAB struct and return to Python.
* Extracting the time and data values as numeric vectors from sim command output and returning these to Python makes data marshaling relatively easy.This example has been tested with MATLAB R2022b and Python 3.8. The following MathWorks products are needed for using this example:
* [MATLAB](https://www.mathworks.com/products/matlab.html);
* [Simulink](https://www.mathworks.com/products/simulink.html);
* [MATLAB Compiler™](https://www.mathworks.com/products/compiler.html);
* [MATLAB Compiler SDK](https://www.mathworks.com/products/matlab-compiler-sdk.html);
* [Simulink Compiler](https://www.mathworks.com/products/simulink-compiler.html);