Writing S-Functions | ![]() ![]() |
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 and subsequent releases; 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:
simulink/src/sfunctmpl_doc.c
. This template S-function file concisely summarizes level 2 S-functions.
mdlInitializeSizes
. In particular, add the 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 */ ssSetInputPortRequiredContiguous(S, 0); Set up the outputs using: if (!ssSetNumOutputPorts(S, 1)) return; ssSetOutputPortWidth(S, 0, width); /* Width of output port one (index 0) */
mdlInitializeConditions
, update it to the following form:
ssGetContStates
. The ssGetX
macro has been removed.
ssGetRealDiscStates(S)
. The ssGetX
macro has been removed.
mdlOutputs
prototype has changed from
mdlUpdate
function prototype has changed from
mdlUpdate
, update it to this form:
mdlDerivatives
, update it to this form:
gcc
on a UNIX system, use these options with the mex
utility.
On a PC, to use the highest warning levels, you must create a project file inside the integrated development environment (IDE) for the compiler you are using. Within the project file, define MATLAB_MEX_FILE
and add
to the path (be sure to build with alignment set to 8).
![]() | Writing Callback Methods | Obsolete Macros | ![]() |