User`s guide
Writing Basic C MEX S-Functions
3-5
To incorporate this S-function into Simulink, create a source f ile called
timestwo.c and place the S-function routines of the above f igure in it. Typing
mex timestwo.c
at the command line directs MATL AB to compile and link the timestwo.c file .
This creat es a dynamically loadable executab le f or Simulink ’s us e.
The resulting executable is referred to as a MEX S-function, where MEX
stands for “ MAT LA B EX ecutable .” The MEX-f ile e xte nsion varies from
platform t o pl atfo rm. For example, in Microso ft Wind ows, the MEX- file
extension is
.dll.
This tabl e sho ws the descript ions o f the S-functio n rout ines re quire d in t he
timestwo S-function.
Table 3-1: S-Function Routines Called
During Simulation of the timestwo Example
S-Function Routine Description
mdlInitializeSizes
Simulink calls this routine while editing
the model to determine the number of
input and output ports. Simulink also
calls it at the start of simulation to
inquire about the sizes of the p orts and
any other objects (such as the number of
states) needed by the S-function.
mdlInitializeSampleTimes
Simulink calls t h is routine to se t the
sample time(s) of the S-function. In this
example, it executes whenever the
driving block executes. Therefore, it has a
single inherited sample time,
SAMPLE_TIME_INHERITED.