User`s guide

3 Writing S-Functions As C-MEX files
3-48
routines. In mdlStart allocate and initialize the memory and place the pointer
to it either in pointer-work vector elements
ssGetPWork(S)[i] = ptr;
or attach it as user data:
ssSetUserData(S,ptr);
In mdlTerminate, free the allocated memory.
S-Function Initialization
The S-function routines discussed so far have dealt with defining the
S-function characteristics, such as the number of input and output ports and
their widths. This section describes how to initialize S-functions. This may
mean initializing the work vectors or the output signal of the S-function. With
the Real-Time Workshop, this may mean initializing hardware. The routines,
mdlStart and mdlInitializeConditions, perform initialization.
mdlStart
The optional routine, mdlStart, is called once at the start of model execution.
Place one time initialization code here.
The synopsis is:
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
}
#endif /* MDL_START */
For an example, see matlabroot/simulink/src/sfun_multiport.c.
mdlInitializeConditions
You can initialize the continuous and discrete states for your S-function block
in the optional S-function routine
mdlInitializeConditions. The initial
states are placed in the state vector,
ssGetContStates(S) and/or
ssGetNumDiscStates(S). You can also perform any other initialization
activities that your S-function may require.