| Writing S-Functions |
 |
S-Function Callback Methods
An S-function comprises a set of S-function callback methods that perform tasks required at each simulation stage. During simulation of a model, at each simulation stage, Simulink calls the appropriate methods for each S-function block in the model. Tasks performed by S-function methods include:
- Initialization -- Prior to the first simulation loop, Simulink initializes the S-function. During this stage, Simulink:
- Initializes the
SimStruct, a simulation structure that contains information about the S-function
- Sets the number and dimensions of input and output ports
- Sets the block sample times
- Allocates storage areas and the
sizes array
- Calculation of next sample hit -- If you've created a variable sample time block, this stage calculates the time of the next sample hit; that is, it calculates the next step size.
- Calculation of outputs in the major time step -- After this call is complete, all the output ports of the blocks are valid for the current time step.
- Update of discrete states in the major time step -- In this call, all blocks should perform once-per-time-step activities such as updating discrete states for next time around the simulation loop.
- Integration -- This applies to models with continuous states and/or nonsampled zero crossings. If your S-function has continuous states, Simulink calls the output and derivative portions of your S-function at minor time steps. This is so Simulink can compute the states for your S-function. If your S-function (C MEX only) has nonsampled zero crossings, Simulink calls the output and zero-crossings portions of your S-function at minor time steps so that it can locate the zero crossings.
| Note
See "How Simulink Works" in the Using Simulin documentation for an explanation of major and minor time steps.
|
| How S-Functions Work | | Implementing S-Functions |  |