How to control simulink simulation from python?

5 vues (au cours des 30 derniers jours)
Indhu Priyadharshini Govindasamy
Réponse apportée : Meet le 12 Déc 2024
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")

Réponses (1)

Meet
Meet le 12 Déc 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!

Catégories

En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by