Technical data

3
Writing Target Language Files
3-32
In general, inlining an S-function is especially useful when:
The time required to execute the contents of the S-function is small in
comparison to the overhead required to call the S-function.
Certain S-function routines are empty (e.g.,
mdlUpdate).
The behavior of the S-function changes between simulation and code
generation. For example, device driver I/O S-functions may read from the
MATLAB workspace during simulation, but read from an actual hardware
address in the generated code.
An Example
Suppose you have a simple S-function that mimics the Gain block with one
input, one output, and a scalar gain. That is,
y = u * p. If the Simulink block’s
name is
foo and the name of the S-function is foogain, the C-coded MEX-file
must contain:
#define S_FUNCTION_NAME foogain
#include "simstruc.h"
#define GAIN mxGetPr(ssGetArg(S,0))[0]
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumContStates (S, 0);
ssSetNumDiscStates (S, 0);
ssSetNumInputs (S, 1);
ssSetNumOutputs (S, 1);
ssSetNumInputArgs (S, 1);
ssSetDirectFeedThrough (S, 1);
ssSetNumSampleTimes (S, 0);
ssSetNumIWork (S, 0);
ssSetNumRWork (S, 0);
ssSetNumPWork (S, 0);
}
static void
mdlOutputs(real_T *y, const real_T *x, const real_T *u,
SimStruct *S, int_T tid)