This example shows two methods to connect a deployed simulation with an external environment using callbacks. Restricting the callbacks to only run at communication steps reduces overhead and improves performance.
The examples require R2023a (or newer) version the following products:
- MATLAB®
- Simulink®
- MATLAB Compiler™
- Simulink Compiler™
The first method uses PostSteFcn callback to get the latest simulation results (response) and update parameter values (stimuli). to use this method, the model should be setup as shown below, with stimuli coming into the model as parameter updates. When the value of the parameter, p, in the Constant block changes, its value is picked up by 'The Algorithm', block.
The second method uses ExternalInputsFcn to get the latest simulation results (response) and specify new input signal values (stimuli). For this method the model should be setup as shown below, with the stimuli coming into the model as an input signal through the root inport block, In1.
The first method is easier to implement (and understand) but limits the stimuli to be constant between communication steps. The second method is more powerful because the input signal between communication steps can be a time series.
The plot below shows that as communication interval is increased, simulation pace increases (i.e., performace improves) due to the reduced callback overhead.


