MATLAB: MATLAB engine in Python hangs when interacting with a Simulink model.

enginehangMATLABpythonsimulink

I'm currently using the MATLAB engine in Python, usually without any problems. However, I'm running into some issues with the program hanging.
Specifically, currently the deadlock is observed when I call this method:
self.eng.eval("val = get_param(fullPath, parameter)")
where 'eng' is a reference to the MATLAB engine, 'fullPath' is the path to a Scope element in my model and 'parameter' = "TimeSpan".
The engine hangs on this call (although I have done many calls to the same function before this one, successfully) for some reason. Sometimes, it does 'go through' as expected. Interestingly, it always appears to be on this specific object, with this specific parameter, if it hangs. As I said, I query many of my objects with different parameters before this call, but even in a test model with only a Scope element, the deadlock often occurs.
However this does not, seem to occur whenever I run it the first time after a reboot of my machine.
When I call the exact same function from within MATLAB, it works fine. Interestingly, when I start a new Python process, and then load the model that my script hangs on, that works fine. I get no error message, it simply hangs. Note that before loading the model, I can perform the call and I get the error correctly. It is only after loading the model with load_system that the call hangs.

Best Answer

This appears to be a graphics related issue which may be associated with old or outdated graphics drivers. MATLAB can have problems with graphics if you are using Simulink models with scopes on older graphics hardware or outdated graphics drivers.
Try to reproduce the issue by adding the "-softwareopengl" flag to the command line parameter to the constructor when you start the MATLAB engine in python. If the engine doesn't hang while doing this then the issue is likely with the graphics drivers.
Please follow the below documentation link:
There are many workarounds and troubleshooting steps on this page. If you want to get the faster graphics functionality working, you can try some of the other troubleshooting steps on this page, eg. updating graphics drivers, etc.