Writing S-Functions | ![]() ![]() |
Initialize the state vectors of this S-function.
Syntax
Arguments
S
SimStruct representing an S-Function block.
Description
Simulink invokes this optional method at the beginning of a simulation. It should initialize the continuous and discrete states, if any, of this S-Function block. Use ssGetContStates
and/or ssGetDiscStates
to get the states. This method can also perform any other initialization activities that this S-function requires.
If this S-function resides in an enabled subsystem configured to reset states, Simulink also calls this method when the enabled subsystem restarts execution. This method can use ssIsFirstInitCond
macro to determine whether it is being called for the first time.
Example
This example is an S-function with both continuous and discrete states. It initializes both sets of states to 1.0:
#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */ #if defined(MDL_INITIALIZE_CONDITIONS) static void mdlInitializeConditions(SimStruct*
S) { int i; real_T *xcont = ssGetContStates(S); int_T nCStates = ssGetNumContStates(S); real_T *xdisc = ssGetRealDiscStates(S); int_T nDStates = ssGetNumDiscStates(S); for (i = 0; i < nCStates; i++) {*
xcont++ = 1.0; } for (i = 0; i < nDStates; i++) {*
xdisc++ = 1.0; } } #endif /* MDL_INITIALIZE_CONDITIONS */
For another example that initializes only the continuous states, see matlabroot
/simulink/src/resetint.c
.
Languages
See Also
mdlStart, ssIsFirstInitCond, ssGetContStates, ssGetDiscStates
![]() | mdlGetTimeOfNextVarHit | mdlInitializeSampleTimes | ![]() |