User`s guide

Overview of the C MEX S-Function Routines
3-43
Example - Defining a Sample Time for a Hybrid Block. This examp le defines sample
times for a hybrid S-Function block.
/* Initialize the sample time and offset. */
static void mdlInitializeSampleTimes(SimStruct *S)
{
/* Continuous state sample time and offset. */
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
/* Discrete state sample time and offset. */
ssSetSampleTime(S, 1, 0.1);
ssSetOffsetTime(S, 1, 0.025);
}
In the second sample time, the offset causes Simulink to call the mdlUpdate
function at these times: 0.025 seconds, 0.125 seconds, 0.225 seconds, and so on,
in increments of 0.1 seconds.
The following statement, which indicates how many sample times are defined,
also appears in the
mdlInitializeSizes function:
ssSetNumSampleTimes(S, 2);
Configuring Work Vectors
If your S-function needs persistent memory storage, use S -function work
vectors instead of static or global variables. If you use static or global variables,
they are used by mult iple instances of your S - function. This occurs when you
have multiple S-Function blocks in a Simulink model and the same S-function
C MEX-file has been specified. The ability to keep track of multiple instances
of an S-function is called re-entrancy.
You can create an S-function that is re-entrant by using work vectors. These
are persistent storage locations that Simulink manages for an S-function.
Integer, floating point (real), pointer, and general data types are supported.
The number of elements in each vector can be specified dynamically as a
function of the number of inputs to the S-function.