User`s guide

3 Writing S-Functions As C-MEX files
3-8
There is more information that the timestwo.c example requires:
Defines and includes The example specifies the name o f the S-function
(
timestwo) and that the S-function is in the level 2 format (for more
information about level 1 and level 2 S-functions, see “Converting Level 1 C
MEX S-Functions to Level 2” on page 3-118). After defining these two items,
the example includes
simstruc.h, which is a header file that gives access to
the
SimStruct data structure and the MATLAB Application Program
Interface (API) functions:
#define S_FUNCTION_NAME timestwo
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
mdlInitializeSizes specifies the following for the timestwo example:
- Zeroparameters—Thismeansthatthe
S-functionparameters fieldofthe
S-functions’s dialog box must be empty. If it contains any parameters,
Simulink will report a parame ter mis mat ch.
- One input port and one output port—The widths of the input and output
port are dynamic ally s ized. Thi s tells Simulink to multip ly each element
of the input signal to the S-function by two and t o place the result in the
output signal. N ote that the default handling for dynamically sized
S-functions for this case (one input and one output) is that the input and
output widths are equal.
- One sample time. You must specify the actual value of the sample time in
the
mdlInitializeSampleTimes routine.
- The code is exception free. Specifying exception free code speeds up
execution of your S-function. Care must be taken when specifying this
option . In gen eral, if yo u r S-f unc t io n isn ’t int er a c tin g wi th MAT L A B, it is
safe t o specif y this opti on. For more deta ils, see “Error H and ling” on page
3–12.
mdlInitializeSampleTimes:
- The sample time is inherit ed from t he driv ing block. Thi s means that the
S-function will run whenever it receives input from the block that is
connecte d to the S-f unc tion block’s in pu t port.