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?

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.