User`s guide

3 Writing S-Functions As C-MEX files
3-10
Creating More Complex C MEX S-Functions
There are numerous S-function routines available for use in C MEX
S-functions. Simulink provides t emplate C MEX-file S-functions that include
statements that define the necessary functions, as well as comments t hat
should help you write the code needed for your S-function block. The template
file,
sfuntmpl.c, which can be found in the directory simulink/src below the
MATLAB root directory, contains commonly used S-function routines. A
template contai ning all available routines (as w e ll as m o r e comments) can be
found in
sfuntmpl.doc in the same directory.
Note We recommend that you use the C MEX-file template when developing
MEX S -functions. See section “Converting Level 1 C MEX S -Functions to
Level 2 on page 3–118.
Statements Required at the Top of S-Functions
ForS-functionsto operateproperly,each sourcemodule of yourS-function that
accesses the
SimStruct must contain the following sequence of defines and
include
#define S_FUNCTION_NAME your_sfunction_name_here
#define SFUNCTION_LEVEL 2
#include "simstruc.h"
Wher e your_sfunction_name_here is the name o f your S- function (i.e., what
you enter in the Simulink S- Functi on block dia lo g). T hese sta te ment s give yo u
access to the
SimStruct data structure that contains p ointers to the data used
by the simulation. The include d code als o defines the ma cros us ed to sto re and
retrieve data in the
SimStruct,describedindetailin“TheCMEXS-Function
SimStruct” on page 3–97. In addition, the code specifies that you are using t he
level 2 format of S-functions.
Note All S-functionsfrom Simulink 1.3 through 2.1 are considered to be level
1 S-functions. They are compatible with Simulink 3.0, but we recommend that
you write new S-functions in the level 2 format.