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.
When compiling your S-function for use with the Real-Time Workshop, simstruc.h
includes the following.
Statements Required at the Bottom of S-Functions
Include this trailer code at the end of your C MEX S-function main module only.
#ifdef MATLAB_MEX_FILE/*
Is this being compiled as MEX-file?*
/ #include "simulink.c"/*
MEX-file interface mechanism*/
#else #include "cg_sfun.h"/*
Code generation registration func*/
#endif
These statements select the appropriate code for your particular application:
simulink.c
is included if the file is being compiled into a MEX-file.
cg_sfun.h
is included if the file is being used in conjunction with the Real-Time Workshop to produce a stand-alone or real-time executable.
![]() | Templates for C S-Functions | The SimStruct | ![]() |