MATLAB: Do I receive an “Undefined function or variable” for “simout” inside an Assertion block callback in Simulink 8.5 (R2015a)

assertiondataloggingpausesimulationcommandsimulinkworkspacewritedatalogs

When we use following commands inside the callback of Assertion block and simulate the model, an error is generated which mentions that the variable SIMOUT cannot be found.
set_param(bdroot,'SimulationCommand','pause');
disp('Simulation paused.');
evalin('base','plot(simout.signals.values)');
disp('Data plotted.');
set_param(bdroot,'SimulationCommand','continue')
However using the following command from MATLAB Command Prompt works fine:
plot(simout.signals.values)

Best Answer

The error message is due to the following missing callback
set_param(bdroot,'SimulationCommand','WriteDataLogs')
Here is what happens without the missing callback to WriteDataLogs:
- The assertion block runs with non-zero input, and executes the callback
- The command "set_param(bdroot,'SimulationCommand','pause')" sets the parameter.
- The command "disp('Simulation paused.')" prints to the command window.
- The command "evalin('base','plot(simout.signals.values)')" fails because simout is not yet in the base workspace.
- The solver sees that parameter SimulationCommand' is 'pause', so it does not start another time step.
Adding the missing callback makes the 'To workspace' block write its data to the workspace immediately, so it is available when the plot command executes.
There is one additional issue with the code supplied in the question: you cannot pause and resume simulation within an Assertion block callback. As mentioned in the documentation, setting "SimulationCommand" to "pause" or "continue" makes a request to the solver - it does not cause immediate action. In this case, the solver will not process that request until after the callback finishes. Thus, throughout the execution of the callback, the "SimulationStatus" is "running." The call to "set_param" that is supposed to set "SimulationCommand" to "continue" will be ignored, and when the callback exits the simulation will pause. Because the execution of the callback happens within one solver step, pausing and resuming is unnecessary here. The correct callback code would be:
 
set_param(bdroot,'SimulationCommand','WriteDataLogs')
evalin('base','plot(simout.signals.values)');
disp('Data plotted.');