mdlInitializeConditions
Initialize the state vectors of this C MEX S-function
Required
No
Languages
C, C++
Syntax
#define MDL_INITIALIZE_CONDITIONS
void mdlInitializeConditions(SimStruct *S)
Arguments
S
SimStruct representing an S-Function block.
Description
The Simulink® engine 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. In a C MEX S-function,
use
and/or ssGetContStates
to access
the states. This method can also perform any other initialization
activities that this S-function requires.ssGetDiscStates
Note
If you have Simulink
Coder™ and you need to ensure that the initialization code in the
mdlInitializeConditions
function is run only once, then move this
initialization code into the mdlStart
method. MathWorks® recommends this code change as a best practice.
If this S-function resides in an enabled subsystem configured
to reset states, the Simulink engine also calls this method when
the enabled subsystem restarts execution. C MEX S-functions can use
the ssIsFirstInitCond
macro to determine whether
the time at which mdlInitializeCondition is called is equal to the
simulation start time.
Note
If you have Simulink
Coder, when generating code for a noninlined
C MEX S-function that contains this method, make sure the method is
not wrapped in a #if defined(MATLAB_MEX_FILE)
statement.
For example:
#define MDL_INITIALIZE_CONDITIONS #if defined(MDL_INITIALIZE_CONDITIONS) && defined(MATLAB_MEX_FILE) static void mdlInitializeConditions(SimStruct *S) { /* Add mdlInitializeConditions code here */ } #endif
The define
statement makes the mdlInitializeConditions
method
available only to a MATLAB® MEX file. If the S-function is not
inlined, the Simulink
Coder product cannot use this method,
resulting in link or run-time errors.
The Simulink engine calls mdlInitializeConditions
prior
to calculating the S-function's input signals. Therefore, since the
input signal values are not yet available, mdlInitializeConditions
should
not use the input signal values to set initial conditions. If your
S-function needs to initialize internal values using the block's input
signals, perform the initialization in mdlOutputs
.
For example, in a C MEX S-function, initializes an IWork vector
with one element in the mdlInitializeSizes
method.
ssSetNumIWork(S, 1);
The IWork vector holds a flag indicating if initial values have
been specified. Initialize the flag's value in the mdlInitializeCondition
method.
static void mdlInitializeConditions(SimStruct *S) { /* The mdlInitializeConditions method is called when the simulation start and every time an enabled subsystem is re-enabled. Reset the IWork flag to 1 when values need to be reinitialized.*/ ssSetIWorkValue(S, 0, 1); }
Check the value of the IWork vector flag in the mdlOutputs
method,
to determine if initial values need to be set. Since the engine has
calculated input values at this point in the simulation, the mdlOutputs
method
can use them to initialize internal values.
static void mdlOutputs(SimStruct *S, int_T tid) { // Initialize values if the IWork vector flag is true. // if (ssGetIWorkValue(S, 0) == 1) { // Enter initialization code here // } // Remainder of mdlOutputs function // }
For a Level-2 MATLAB S-function, use a DWork vector instead of an IWork vector in the previous example.
Examples
This example initializes both a continuous and discrete state 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 resetint.c
.
See Also
mdlStart
, mdlOutputs
, ssIsFirstInitCond
, ssGetContStates
, ssGetDiscStates
, ssGetTStart
, ssGetT
, InitializeConditions
Version History
Introduced before R2006a