User`s guide

3 Writing S-Functions As C-MEX files
3-36
Setting Sample Times for C MEX S-Functions
Simulink supports blocks that execute at different rates. There are two
methods by which you can specify the rates (i.e., sample times):
Block-based sample times
Port-based sample times
In the case of block-based sample times, your S-function specifies all the
sample rates of the block and processes inputs and outputs at the fastest rate
specified if a ll the s ample times are integer multiples of the fastest sample
time. (If your sample times are not multiples of each other, Simulink behaves
differently. See “ Sa mple Time Colors” in chapter 9 of Using Simulink for more
information.) When using port-based sample times, your S-function specifies
thesampletimeforeachinputandoutputport.Tocomparethetwomethods,
consider two sample rates, 0.5 and 0.25 seconds respectively:
In the block-based method, selecting 0.5 and 0.25 would direct the block to
execute inputs and outputs at 0.25 second increments.
.In the port-based method, you could set the input port to 0.5 and the output
port to 0.25, and the block would execute inputs at 2Hz and outputs at 4Hz.
You should use port-based sample times if your application requires unequal
sample rates for input and output execution or if you don’t want the overhead
associated with running input and output ports at the highest sample rate of
your block.
In typical applications, you will specify only one block-based sample time.
Advanced S-functions may require the specification of port-based or multiple
block sample times.
Block-based Sample Times
The next two sections discuss how to specify block-based sample times. You
must specify information in
mdlInitializeSizes
mdlInitializeSampleTimes
A third sections presents a simple example that shows how to specify sample
times in
mdlInitializeSampleTimes.