Nicklas S. answered 10/13/24
Computer Science/Programming/English Tutor
Somewhat outdated question (back in 2019), but I'll answer just in case you're still running into issues 5 years later:
The set_param function is typically used to change the parameters of a sim while it's running, but the script is running over your command and waiting for the sim to complete before implementing the function, thus your error. This is due to the blocking behavior of the set_param function: essentially, you need at least 3 arguments to successfully pass this function: a path to the block (Block handle), parameter name, and the new value you want to assign to the parameter. In your original script, you did not specify the path to your block.
To avoid this, you could use either a callback or a timer in the first script to check for changes periodically in your second script, in theory overstepping the blocking behavior of set_param. You could also just open a second instance of Matlab for your second script and use an eval() function to communicate changes between the two scripts, which would avoid the need for set_param altogether.
Running without display error:
The simulation you're trying to run in MatLab most likely doesn't have a GUI (user display/interface). This is normal and expected, as some Simulink features do require a display. There are a couple of potential fixes, such as using simulink.sdi to manage sims that don't need a user interface or running the sim in the background with the simulationcommand 'Start' and 'Continue' commands.
Examples:
1st script:
import matlab.engine
import time
eng = matlab.engine.start_matlab()
eng.eval("load_system('scheme123')", nargout=0)
eng.eval("set_param('scheme123', 'SimulationCommand', 'Start')", nargout=0)
# You could also have a loop here to keep the script alive
while True:
time.sleep(1)
2nd Code:
import matlab.engine
import time
eng = matlab.engine.connect_matlab('my_sim123')
# Change the parameter periodically or based on a condition
while True:
eng.eval("set_param('scheme123/PID', 'P', '15')", nargout=0)
time.sleep(1) # You can change the frequency as required here