| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
No
#define MDL_INITIALIZE_CONDITIONS
void mdlInitializeConditions(SimStruct *S)
InitializeConditions(s)
Instance of Simulink.MSFcnRunTimeBlock class representing the Level-2 M-File S-Function block.
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 ssGetContStates and/or ssGetDiscStates to access the states. In a Level-2 M-file S-function, use the ContStates or Dwork run-time object methods to access the continuous and discrete 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, 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 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 Real-Time Workshop product cannot use this method, resulting in link or run-time errors. |
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 M-file S-function, use a DWork vector instead of an IWork vector in the previous example.
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.
This example initializes both a continuous and discrete state to 1.0. Level-2 M-file S-functions store discrete states in their DWork vectors.
function InitializeConditions(s) s.ContStates.Data(1) = 1; s.Dwork(1).Data = 1; % endfunction
C, C++, M
mdlStart, mdlOutputs, ssIsFirstInitCond, ssGetContStates, ssGetDiscStates, ssGetTStart, ssGetT
![]() | mdlGetTimeOfNextVarHit | mdlInitializeSampleTimes | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |