Main Content

S-Function Callback Methods

Callback Methods Overview

Every S-function must implement a set of methods, called callback methods, that the Simulink® engine invokes when simulating a model that contains the S-function.

The S-function callback methods perform tasks required at each simulation stage. During simulation of a model, at each simulation stage the Simulink engine calls the appropriate methods for each S-Function block in the model.

Tasks performed by S-function callback methods include:

  • Initialization — Prior to the first simulation loop, the engine initializes the S-function, including:

    • Initializing the SimStruct, a simulation structure that contains information about the S-function

    • Setting the number and dimensions of input and output ports

    • Setting the block sample times

    • Allocating storage areas

  • Calculation of next sample hit — If you 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 block output ports are valid for the current time step.

  • Update of discrete states in the major time step — In this call, the block performs once-per-time-step activities such as updating discrete states.

  • Integration — This applies to models with continuous states and/or nonsampled zero crossings. If your S-function has continuous states, the engine calls the output and derivative portions of your S-function at minor time steps. This is so the solvers can compute the states for your S-function. If your S-function has nonsampled zero crossings, the engine also 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 Simulation Phases in Dynamic Systems for an explanation of major and minor time steps.

Some callback methods are optional. The engine invokes an optional callback only if the S-function defines the callback.

Callback Methods for C MEX S-Functions

Required Callback Methods

C MEX S-functions must implement the following callback methods:

  • mdlInitializeSizes – Specifies the sizes of various parameters in the SimStruct, such as the number of output ports for the block.

  • mdlInitializeSampleTimes – Specifies the sample time(s) of the block.

  • mdlOutputs – Calculates the output of the block.

  • mdlTerminate – Performs any actions required at the termination of the simulation. If no actions are required, this function can be implemented as a stub.

Optional Callback Methods

The following callback methods are optional. The engine invokes an optional callback only if the S-function defines the callback.

Callback Methods for Level-2 MATLAB S-Functions

Required Callback Methods

Level-2 MATLAB® S-functions must implement the following callback methods:

  • setup – Specifies the sizes of various parameters in the SimStruct, such as the number of output ports for the block.

  • Outputs – Calculates the output of the block.

  • Terminate – Performs any actions required at the termination of the simulation. If no actions are required, this function can be implemented as a stub.

For information on writing callback methods, see Write Level-2 MATLAB S-Functions.

Optional Callback Methods

The following callback methods are optional. The engine invokes an optional callback only if the S-function defines the callback.

See Also

| | |

Related Topics