MATLAB: How to realize an S-Function that can switch between having continuous states and discrete states based on the sample time inherited by the S-Function block in Simulink 7.3 (R2009a)

continuousdiscretedynamically_sizedmdlsetworkwidthss-functionsimulinksssetnumcontstatessssetnumdiscstatesstates

I would like to create an S-Function that has the ability to switch between being continuous and discrete. I set the ssSetNumContStates and ssSetNumDiscStates macros to DYNAMICALLY_SIZED and specify the real number of states in the mdlSetWorkWidths method. This does not work when the S-Function inherits a discrete sample time and I receive the following error message:
S-function edge in 'test_edge/S-Function' must have a continuous sample time since it has continuous states.

Best Answer

The ability to create an S-Function that can switch between having continous states and discrete states is currently not available in Simulink 7.3 (R2009a) and prior versions.
To workaround this issue you can specify a parameter for the S-Function block that will set the number of discrete and continuous states in the mdlInitializeSizes method of the S-Function:
if(!(*mxGetPr(ssGetSFcnParam(S,0))))
ssSetNumContStates(S, 1);