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 CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by