User`s guide

3 Writing S-Functions As C-MEX files
3-118
Converting Level 1 C MEX S-Functions to Level 2
Level 2 S-functions were introduced with Simulink 2.2. Level 1 S-functions
refer to S-functions that were written to work with Simulink 2.1 and previous
releases. Level 1 S-functions are compatible with Simulink 2.2; you can use
them in new models without making any code changes. However, to take
advantage of new features in S-functions, level 1 S-functions must be updated
to level 2 S-functions. Here are some guidelines:
Start by looking at
simulink/src/sfunctmpl.doc. This template S-function
file concisely summarizes level 2 S-functions.
At the top of your S-function file, add this define:
#define S_FUNCTION_LEVEL 2
Update the contents of mdlIntializeSizes, in particular add t he following
error handling for the number of S-function parameters:
ssSetNumSFcnParams(S, NPARAMS); /*Number of expected parameters*/
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}
Set up the inputs using:
if (!ssSetNumInputPorts(S, 1)) return; /*Number of input ports */
ssSetInputPortWidth(S, 0, width); /* Width of input
port one (index 0)*/
ssSetInputPortDirectFeedThrough(S, 0, 1); /* Direct feedthrough
or port one */
Set up t he outputs using:
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, width); /* Width of output port
one (index 0) */