How to control simulink simulation from python?

5 views (last 30 days)
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")

Answers (1)

Meet
Meet on 12 Dec 2024
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!

Community Treasure Hunt

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

Start Hunting!