MATLAB: Can’t I continue the simulation using ‘PauseFcn’ callback


I am pausing my simulation using an assertion block, as shown in below documentation example:
I tried to add below simulation command to the 'PauseFcn' callback to resume the simulation automatically, but it doesn't work:
However, I can continue the simulation if I enter the command manually at the MATLAB Command Prompt. Why is this?

Best Answer

Since MATLAB Release 2016a, it is not possible the continue a simulation using 'PauseFcn' callback anymore.
We disallowed calling "set_param(gcs,'SimulationCommand','continue')" from the 'PauseFcn' callback, as this is the kind of thing that can easily end up in an infinite recursion if not done properly.
There is currently no workaround to achieve the old behavior; so the best way to move things forward would be to look into other alternate workflows that would be more robust to achieve what you really need.
For example, if you perform co-simulation, an S-Function could do something similar to what the 'PauseFcn' callback is doing. You would not need to pause, the S-Function would call the external environment, wait for it to do its thing and when it is done, it would keep moving forward. That would be a lot more robust and would probably be faster too.
If it is absolutely necessary to retain this workflow, please consider the following workaround in the "PauseFcn" callback:
xx = timer('ExecutionMode','singleShot','StartDelay',0.1,'TimerFcn','set_param(bdroot, ''SimulationCommand'', ''continue'')');
In summary, one creates a timer object and attaches the "continue" function, and then start the timer which will then continue the simulation after the "PauseFcn" callback is exited.