How to execute a simulink simulation stepwise from python without a display?

6 次查看(过去 30 天)
I have a .slx file with a simulink simulation that i run from python code. I need to execute it step-wise and adjust parameters after each step.
My code is executed on a server to which i have remote access via SSH. The goal is to execute multiple variants of the simulation in parallel and disconnect from the server while the simulations are executed.
The 'set_param' command can not be used for this due to having no display. According to https://de.mathworks.com/help/simulink/slref/set_param.html the 'sim' command should be used in this case. While
sim_out = eng.sim('simulation_name')
works fine and returns the desired values it dows not work with
sim_out = eng.sim('simulation_name','StartTime','0.0','StopTime','10.0')
which would be needed for a stepwise execution. Or rather: I can execute it, but the return value is an empty array of shape (1,1,0).
I noticed that the second command works perfectly fine when put into the matlab command prompt instead of executing it via python.
My aim is to have something as following:
import matlab.engine as me
eng = me.start_matlab('-nodesktop -nosplash -nodisplay')
step_size = 10.0
steps = 1500
for i in steps:
action = get_action(state)
sim_out = sim('simulation_name','Action',str(action),'StartTime',str(i * step_size),'StopTime',str((i+1) * step_size))
state = np.array(eng.workspace['State'])
end

回答(1 个)

Anjaneyulu Bairi
Anjaneyulu Bairi 2023-9-6
I understand that you are trying to execute the Simulink model in stepwise manner. Refer the below MATLAB Answer which solves your query.
Additionally, refer the below link which explains how to control execution of Simulink model in python.
Hope the provided information helps in resolving your query.

类别

Help CenterFile Exchange 中查找有关 Python Client Programming 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by