Call-simulink-from-Python

バージョン 1.0.0 (292 KB) 作成者: Weiwu Li
Examples to show the two options to simulate a Simulink model from Python
ダウンロード: 50
更新 2023/3/13

Simulate a Simulink® model from Python®

This example illustrates two ways to simulate a Simulink model (named the_model) via a wrapper MATLAB® function (named sim_the_model) from Python.

the_model

The first approach, shown in call_sim_the_model_using_matlab_engine.py, uses the MATLAB Engine API for Python to call the wrapper function sim_the_model.m multiple times passing in different parameters and external input signals.

results_mle

The second approach uses MATLAB Compiler SDK and Simulink Compiler to first build a Python package around the wrapper function sim_the_model.m as shown in build_python_package_around_sim_the_model.m. We can then call this package to run the warpper function sim_the_model.m multiple times passing in different parameters and external input signals as show in call_sim_the_model_using_matlab_runtime.py.

results_mrt

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 setup

The model the_model.slx and the wrapper MATLAB function sim_the_model.m illustrate implementation choices that make data marshaling between Python and sim 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:

引用

Weiwu Li (2024). Call-simulink-from-Python (https://github.com/mathworks/Call-Simulink-from-Python/releases/tag/v1.0.0), GitHub. 取得済み .

MATLAB リリースの互換性
作成: R2022b
R2022b 以降 R2023a 以前と互換性あり
プラットフォームの互換性
Windows macOS Linux
タグ タグを追加

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
バージョン 公開済み リリース ノート
1.0.0

この GitHub アドオンでの問題を表示または報告するには、GitHub リポジトリにアクセスしてください。
この GitHub アドオンでの問題を表示または報告するには、GitHub リポジトリにアクセスしてください。