Writing S-Functions    

S-Function Source File Requirements

This section describes requirements that every S-function source file must meet to compile correctly. The S-function templates meet these requirements.

Statements Required at the Top of S-Functions

For S-functions to operate properly, each source module of your S-function that accesses the SimStruct must contain the following sequence of defines and include

where your_sfunction_name_here is the name of your S-function (i.e., what you enter in the Simulink S-Function block dialog). These statements give you access to the SimStruct data structure that contains pointers to the data used by the simulation. The included code also defines the macros used to store and retrieve data in the SimStruct, described in detail in Converting Level 1 C MEX S-Functions to Level 2. In addition, the code specifies that you are using the level 2 format of S-functions.

The following headers are included by matlabroot/simulink/include/simstruc.h when compiling as a MEX-file.

Table 3-1: Header Files Included by simstruc.h When Compiling as a MEX-File
Header File
Description
matlabroot/extern/include/tmwtypes.h
General data types, e.g., real_T
matlabroot/extern/include/mex.h

MATLAB MEX-file API routines

matlabroot/extern/include/matrix.h

MATLAB MEX-file API routines

When compiling your S-function for use with the Real-Time Workshop, simstruc.h includes the following.

Table 3-2: Header Files Included by simstruc.h When Used
by the Real-Time Workshop
Header File
Description
matlabroot/extern/include/tmwtypes.h
General types, e.g., real_T
matlabroot/rtw/c/libsrc/rt_matrx.h
Macros for MATLAB API routines

Statements Required at the Bottom of S-Functions

Include this trailer code at the end of your C MEX S-function main module only.

These statements select the appropriate code for your particular application:


  Templates for C S-Functions The SimStruct