Use one of the provided C MEX S-function templates as a starting point for creating your
own S-function. The templates contain skeleton implementations of callback methods with
comments that explain their use. The template file,
sfuntmpl_basic.c, contains commonly used S-function routines. A
template containing all available routines (as well as more comments) can be found in
sfuntmpl_doc.c in the same folder.
We recommend that you use the C MEX file template when developing MEX S-functions.
This section describes requirements that every S-function source file must meet to compile correctly. The S-function templates meet these requirements.
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
#define S_FUNCTION_NAME your_sfunction_name_here #define S_FUNCTION_LEVEL 2 #include "simstruc.h"
your_sfunction_name_here is the name of your
S-function (i.e., what you enter in the S-Function Block Parameters dialog box). 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 Convert Level-1 C MEX S-Functions. In
addition, the code specifies that you are using the Level-2 S-function format.
All S-functions from Simulink® version 1.3 through version 2.1 are considered to be Level-1 S-functions. They are compatible with newer versions of the software, but we recommend that you write new S-functions in the Level-2 format.
The following headers are included by
simstruc.h when compiling as a MEX file.
Header Files Included by simstruc.h When Compiling as a MEX File
|General data types, e.g., |
|MATLAB® MEX file API routines to interface MEX files with the MATLAB environment|
|MATLAB External Interface API routines to query and manipulate MATLAB matrices|
When compiling your S-function for use with the Simulink
simstruc.h includes the following.
Header Files Included by simstruc.h When Used by the Simulink Coder Product
Your S-function must implement the following functions (see Configure C/C++ S-Function Features ):
specifies the sizes of various parameters in the
SimStruct, such as
the number of output ports for the block.
specifies the sample time(s) of the block.
output of the block.
actions required at the termination of the simulation. If no actions are required,
this function can be implemented as a stub.
Your S-function must include the following trailer code at the end of the 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:
simstruc.h is a C language header file that defines the
SimStruct data structure and its access macros. It encapsulates all the
data relating to the model or S-function, including block parameters and outputs.
There is one
SimStruct data structure allocated for the Simulink model. Each S-function in the model has its own
associated with it. The organization of these
SimStructs is much like a
folder tree. The
SimStruct associated with the model is the
SimStruct associated with an S-function
is a child
The Simulink product provides a set of macros that S-functions can use to access the fields
SimStruct. See About SimStruct Functions for more information.
tmwtypes.h is a C language header file that defines a set of data types
used in the S-function template and in the
SimStruct. These data types,
uint32_T, etc., provide a way to
switch between different data types for 16, 32, and 64 bit systems, allowing greater
platform independence and flexibility.
S-functions are not required to use these data types. For example, you can edit the
csfunc.c and change
int. If you
compile and simulate the S-function, the results will be identical to the results using the
previous data types.
Your S-function can be compiled in one of three modes, defined either by the
mex command or by the Simulink
Coder product when the S-function is built:
MATLAB_MEX_FILE — Indicates that the S-function is being
built as a MEX file for use with the Simulink product.
RT — Indicates that the S-function is being built with the
Coder product for a real-time application using a fixed-step solver.
NRT — Indicates that the S-function is being built with
Coder product for a non-real-time application using a variable-step
The build process you use automatically defines the mode for your S-function.