Writing S-Functions | ![]() ![]() |
S-Function Concepts
Understanding these key concepts should enable you to build S-functions correctly:
Direct Feedthrough
Direct feedthrough means that the output (or the variable sample time for variable sample time blocks) is controlled directly by the value of an input port. A good rule of thumb is that an S-function input port has direct feedthrough if:
mdlOutputs
or flag==3
) is a function of the input u
. That is, there is direct feedthrough if the input u
is accessed in mdlOutputs
. Outputs can also include graphical outputs, as in the case of an XY Graph scope.
mdlGetTimeOfNextVarHit
or flag==4
) of a variable sample time S-function accesses the input u
.
An example of a system that requires its inputs (i.e., has direct feedthrough) is the operation , where u is the input, k is the gain, and y is the output.
An example of a system that does not require its inputs (i.e., does not have direct feedthrough) is this simple integration algorithm
where x is the state, is the state derivative with respect to time, u is the input, and y is the output. Note that
is the variable that Simulink integrates. It is very important to set the direct feedthrough flag correctly because it affects the execution order of the blocks in your model and is used to detect algebraic loops.
Dynamically Sized Arrays
S-functions can be written to support arbitrary input dimensions. In this case, the actual input dimensions are determined dynamically when a simulation is started by evaluating the dimensions of the input vector driving the S-function. The input dimensions can also be used to determine the number of continuous states, the number of discrete states, and the number of outputs.
M-file S-functions can have only one input port and that input port can accept only one-dimensional (vector) signals. However, the signals can be of varying widths.Within an M-file S-function, to indicate that the input width is dynamically sized, specify a value of -1 for the appropriate fields in the sizes
structure, which is returned during the mdlInitializeSizes
call. You can determine the actual input width when your S-function is called by using length(u)
. If you specify a width of 0, the input port is removed from the S-function block.
A C S-function can have multiple I/O ports and the ports can have different dimensions. The number of dimensions and the size of each dimension can be determined dynamically.
For example, the following illustration shows two instances of the same S-Function block in a model.
The upper S-Function block is driven by a block with a three-element output vector. The lower S-Function block is driven by a block with a scalar output. By specifying that the S-Function block has dynamically sized inputs, the same S-function can accommodate both situations. Simulink automatically calls the block with the appropriately sized input vector. Similarly, if other block characteristics, such as the number of outputs or the number of discrete or continuous states, are specified as dynamically sized, Simulink defines these vectors to be the same length as the input vector.
C S-functions give you more flexibility in specifying the widths of input and output ports. See Creating Input and Output Ports.
![]() | Implementing S-Functions | Setting Sample Times and Offsets | ![]() |