MATLAB: Is there an example available on how to initialize the states in a C-MEX S-function via input signals

c - mexfrominitializeinputs-functionsignalsimulinkstates

I would like an example on how to initialize the states in a C-MEX S-function via input signals.

Best Answer

Following is an example showing how to initialize the states in a C-MEX S-function via input signals in Simulink.
In this example the Van der Pol equations are used. To initialize the two states x1 and x2 in the equations via input signals the mdlInitializeConditions has been used. If you open up the S-function source file, you will notice the following lines in mdlInitializeConditions:
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs0 = ssGetInputPortRealSignalPtrs(S,0);
InputRealPtrsType uPtrs1 = ssGetInputPortRealSignalPtrs(S,1);
/* init x1 */
x[0] = *uPtrs0[0];
/* init x2 */
x[1] = *uPtrs1[0];
Here at the beginning of simulation the input signals *uPtrs0 and *uPtrs1 are assigned to the states x1 and x2.