Why do I receive an "Undefined function or variable" for "simout" inside an Assertion block callback in Simulink 8.5 (R2015a)?
3 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2009-6-27
编辑: MathWorks Support Team
2016-10-26
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)
采纳的回答
MathWorks Support Team
2016-10-26
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.');
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Model, Block, and Port Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!