MATLAB Answers

How to run Simulation for certain amount of time

9 views (last 30 days)
Hello,
i want to run my Simulation for a certain amount of time and then pause it. I tried with an assertion block which works fine but if i start the simulation through python the assertion block doesn't stop the simulation. Is there a work around?
kind regards

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 11 May 2021
Specify the start time and stop time either directly through numerical value, or through variables and then set the value for the variables.
  3 Comments
Peter Pallasch
Peter Pallasch on 11 May 2021
First thank you for your help!
Actually it was very hard to find something on this topic. I was seaching for days but i found an article. For everyone who has the same problem. You just start/pause the Simulation by setting the parameters directly. No need for the sim command.
Here is my example code:
sys.path.append("c:\work\matlab19bPy36")
eng = matlab.engine.start_matlab("-desktop")
eng.run(nargout=0)
eng.eval("model = '{}'".format("modelname"),nargout=0)
eng.eval("load_system(model)",nargout=0)
print("Initialized Model")
#Start Simulation and then Instantly pause
eng.set_param("modelname",'SimulationCommand','start','SimulationCommand','pause',nargout=0)
while True:
eng.set_param("modelname",'SimulationCommand','continue',nargout=0)
print("Continue")
time.sleep(30)
eng.set_param("modelname",'SimulationCommand','pause',nargout=0)
print("stopped")
time.sleep(30)
Here is a link to the article that helped me. The guy build a little controller to control the simulation via python:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!