0

I am using the MATLAB Engine API for Python. I have a shared engine in a Python script and then another Python script connected to this shared engine. I would like to ask whether it is possible to change the parameter of the running simulation from Simulink using set_param command. It looks like my following solution does not work. The set_param command in the 2nd script is waiting until sim() command from the 1st script is finished. Thanks in advance.

1st script:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.eval("matlab.engine.shareEngine('my_sim123')")
eng.eval("load_system('scheme123')",nargout=0)
eng.eval("sim('scheme123')")

2nd script:

import matlab.engine
eng = matlab.engine.connect_matlab('my_sim123')
eng.eval("set_param('scheme123/PID', 'P', '15')",nargout=0)

UPDATE: I tried to use set_param('scheme123', 'SimulationCommand','Start') instead, but the following error occured: "You cannot use set_param to run a simulation in a MATLAB session that does not have a display." Is there any other way to change the parameter of the running sumulation with no display?

user3618276
  • 91
  • 2
  • 10

1 Answers1

1

This isn't an issue with the Python API. The sim command is blocking - even when used from within MATLAB itself.

To over come this you need to start the simulation using

set_param(gcs,'SimulationCommand','Start')

That will run the simulation to completion. If you need/want to stop it prematurely then use

set_param(gcs,'SimulationCommand','Stop')
Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • I tried to use `eng.set_param('scheme123', 'SimulationCommand','Start',nargout=0)` instead, but the following error occured: "You cannot use set_param to run a simulation in a MATLAB session that does not have a display." Is there any other way to change the parameter of the running sumulation with no display? – user3618276 Mar 24 '17 at 15:28
  • You'll need to start the MATLAB engine with the desktop open: `matlab.engine.start_matlab('-desktop')`. I don't believe you have any other options. You can also use `-minimize` and `-nosplash` to avoid the nuicanse of the desktop being displayed. – Phil Goddard Mar 24 '17 at 16:43