Writing S-Functions    

Block-Based Sample Times

The next two sections discuss how to specify block-based sample times. You must specify information in

A third sections presents a simple example that shows how to specify sample times in mdlInitializeSampleTimes.

Specifying the Number of Sample Times in mdlInitializeSizes.   To configure your S-function block for block-based sample times, use

where numSampleTimes > 0. This tells Simulink that your S-function has block-based sample times. Simulink calls mdlInitializeSampleTimes, which in turn sets the sample times.

Setting Sample Times and Specifying Function Calls in mdlInitializeSampleTimes

mdlInitializeSampleTimes is used to specify two pieces of execution information:

The sample times are specified as pairs [sample_time, offset_time] by using these macros

where sampleTimePairIndex starts at 0.

The valid sample time pairs are (upper-case values are macros defined in simstruc.h).

Alternatively, you can specify that the sample time is inherited from the driving block in which case the S-function can have only one sample time pair

or

The following guidelines may help aid in specifying sample times:

If your function has no intrinsic sample time, then you must indicate that it is inherited according to the following guidelines:

To check for a sample hit during execution (in mdlOutputs or mdlUpdate), use the ssIsSampleHit or ssIsContinuousTask macro. For example, if your first sample time is continuous, then you used the following code fragment to check for a sample hit. Note that you would get incorrect results if you used ssIsSampleHit(S,0,tid).

If, for example, you wanted to determine if the third (discrete) task has a hit, then you would use the following code-fragment.

Example: mdlInitializeSampleTimes

This example specifies that there are two discrete sample times with periods of 0.01 and 0.5 seconds.


 Sample Times Port-Based Sample Times