Matlab S-Functions: Where to use ssSetPWorkValue()?
Matlab’s ssSetPWorkValue()
may not be used in the mdlInitializeSizes
function of an S-Function. This is because the mdlInitializeSizes
function is called before the S-Function’s work vectors are allocated.
Using it in mdlInitializeSizes()
will result in a segmentation fault, leading to a Matlab/Simulink crash.
The correct place to use ssSetPWorkValue()
is in the mdlStart
function. This function is called after the work vectors have been allocated and is the first function where you can safely use ssSetPWorkValue()
.
static void mdlStart(SimStruct *S)
{
// Allocate memory for the pointer work vector
ssSetPWorkValue(S, 0, new MyDataStructure());
}