How to control simulink simulation from python?

7 ビュー (過去 30 日間)
Indhu Priyadharshini Govindasamy
Indhu Priyadharshini Govindasamy 2021 年 11 月 26 日
回答済み: Meet 2024 年 12 月 12 日
I want to control simulink simulation from python so i used matlab engine api and refered this link aswell https://de.mathworks.com/help/matlab/matlab-engine-for-python.html.
First i want to open simulink and run simulink model which i already have.i tried writing python script to open simulink but nothing is working.Could any one please help me to do this?
and i tried the code which is written here to open simulink but i dont know what is the problem and where exactly to save the simulink model?
import matlab.engine
eng=matlab.engine.start_matlab()
eng.sim("mysimulinkmodelname")

回答 (1 件)

Meet
Meet 2024 年 12 月 12 日
Hi Indhu,
You can use the following code to open your Simulink model and run it using the "sim" function. To view the output after execution, it is recommended to include an 'input' function in the Python script to prevent the model from closing immediately after the simulation.
import matlab.engine
# Start MATLAB engine
eng = matlab.engine.start_matlab()
eng.addpath(r"C:\Users\Meet\Downloads")
# Load the Simulink model
model_name = "myModel"
eng.open_system(model_name, nargout = 0)
# Run the simulation
eng.sim(model_name)
input("Press Enter to stop the simulation and close MATLAB...")
I hope this helps resolve your issue!

カテゴリ

Help Center および File ExchangeCall MATLAB from Python についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by