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 section 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:

You specify the sample times as pairs [sample_time, offset_time], using these macros

where sampleTimePairIndex starts at 0.

The valid sample time pairs are (uppercase 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 might help in specifying sample times:

If your function has no intrinsic sample time, 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 get incorrect results if you use ssIsSampleHit(S,0,tid).

If, for example, you wanted to determine whether the third (discrete) task has a hit, 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 Specifying Port-Based Sample Times