Writing S-Functions    

Multirate S-Function Blocks

In a multirate S-Function block, you can encapsulate the code that defines each behavior in the mdlOutputs and mdlUpdate functions with a statement that determines whether a sample hit has occurred. The ssIsSampleHit macro determines whether the current time is a sample hit for a specified sample time. The macro has this syntax

where S is the SimStruct, st_index identifies a specific sample time index, and tid is the task ID (tid is an argument to the mdlOutputs and mdlUpdate functions).

For example, these statements specify three sample times: one for continuous behavior and two for discrete behavior.

In the mdlUpdate function, the following statement encapsulates the code that defines the behavior for the sample time of 0.75 second.

The second argument, 1, corresponds to the second sample time, 0.75 second.

Example of Defining a Sample Time for a Continuous Block

This example defines a sample time for a block that is continuous.

You must add this statement to the mdlInitializeSizes function.

Example of Defining a Sample Time for a Hybrid Block

This example defines sample times for a hybrid S-Function block.

In the second sample time, the offset causes Simulink to call the mdlUpdate function at these times: 0.025 second, 0.125 second, 0.225 second, and so on, in increments of 0.1 second.

The following statement, which indicates how many sample times are defined, also appears in the mdlInitializeSizes function.


  Specifying Port-Based Sample Times Synchronizing Multirate S-Function Blocks