Main Content

S-Function Features and Limitations

S-Function Features

The following tables give overviews of the features supported by different types of S-functions. The first table focuses on handwritten S-functions. The second table compares the features of S-functions automatically generated by the S-Function Builder or Legacy Code Tool.

Features of Hand-Written S-Functions

FeatureLevel-2 MATLAB S-FunctionHandwritten C MEX S-Function
Data typesSupports any data type supported by Simulink® software, including fixed-point types.Supports any data type supported by Simulink software, including fixed-point types.
Numeric typesSupports real and complex signals.Supports real and complex signals.
Frame supportSupports framed and unframed signals.Supports framed and unframed signals.
Port dimensionsSupports scalar, 1-D, and multidimensional input and output signals.Supports scalar, 1-D, and multidimensional input and output signals.
S-function APISupports a larger set of the S-function API. See Level-2 MATLAB S-Function Callback Methods for a list of supported methods.Supports the entire S-function API.
Code generation supportRequires a handwritten TLC file to generate code.Natively supports code generation. Requires a handwritten TLC file to inline the S-function during code generation.
Simulink Accelerator modeProvides the option to use a TLC file in Accelerator mode, instead of running interpretively.Provides the option to use a TLC or MEX file in Accelerator mode.
Model referenceSupports Normal and Accelerator mode simulations when used in a referenced model. Requires a TLC file for Accelerator mode.Provides options for sample time inheritance and Normal mode support when used in a referenced model. See Model Reference Requirements and Limitations.
Simulink.
AliasType
and Simulink.
NumericType
support
Supports Simulink.NumericType and Simulink.AliasType classes (see Configure Custom Data Types).Supports all of these classes (see Configure Custom Data Types).
Bus input and output signalsDoes not support bus input or output signals.Supports nonvirtual bus input or output signals.
Tunable and run-time parametersSupports tunable and run-time parameters.Supports tunable and run-time parameters.
Work vectorsSupports DWork vectors (see Using DWork Vectors in Level-2 MATLAB S-Functions).Supports all work vector types (see Types of DWork Vectors).

Features of Automatically Generated S-Functions

FeatureS-Function BuilderLegacy Code Tool
Data typesSupports any data type supported by Simulink software, including fixed-point types.Supports all built-in data types. To use a fixed-point data type, you must specify the data type as a Simulink.NumericType. You cannot use a fixed-point type with unspecified scaling.
Numeric typesSupports real and complex signals.Supports complex signals only for built-in data types.
Frame supportSupports framed and unframed signals.Does not support frame-based signals.
Port dimensionsSupports scalar, 1-D, and multidimensional input and output signals.Supports scalar, 1-D, and multidimensional input and output signals.
S-function APISupports creation of custom mdlInitializeSizes, mdlInitializeSampleTimes, mdlDerivatives, mdlUpdate, and mdlOutputs. Also allows for automatic generation of mdlStart and mdlTerminate.Supports mdlInitializeSizes, mdlInitializeSampleTimes, mdlStart, mdlInitializeConditions, mdlOutputs, and mdlTerminate.
Code generation supportNatively supports code generation. Also, automatically generates a TLC file for inlining the S-function during code generation.Natively supports code generation optimized for embedded systems. Also, automatically generates a TLC file that supports expression folding for inlining the S-function during code generation.

Accelerator mode

Uses a TLC file in Accelerator mode, if the file was generated. Otherwise, uses the MEX file.Provides the option to use a TLC or MEX file in Accelerator mode.
Model referenceUses default behaviors when used in a referenced model.Uses default behaviors when used in a referenced model.
Simulink.AliasType and Simulink.NumericTypeDoes not support these classes.Supports Simulink.AliasType and Simulink.NumericType.
Bus input and output signalsSupports bus input and output signals. See sfbuilder_bususage for an example.Supports bus input and output signals. You must define a Simulink.Bus object in the MATLAB® workspace that is equivalent to the structure of the input or output used in the legacy code. Does not support bus parameters.
Tunable and run-time parametersSupports tunable parameters only during simulation. Supports run-time parameters.Supports tunable and run-time parameters.
Work vectorsDoes not provide access to work vectors.Supports DWork vectors with the usage type SS_DWORK_USED_AS_DWORK. See Types of DWork Vectors for a discussion on the different DWork vector usage types.

S-Function Limitations

The following table summarizes the major limitations of the different types of S-functions.

ImplementationLimitations
Level-2 MATLAB S-functions
  • Does not support bus input and output signals.

  • Cannot incorporate legacy code during simulation, only during code generation through a TLC file.

Handwritten C MEX S-functionSupports model reference with some limitations. See S-Functions in Referenced Models.
S-Function Builder
  • Generates S-function code using a wrapper function which incurs additional overhead.

  • Does not support the following S-function features:

    • Work vectors

    • Port-based sample times

    • Multiple sample times or a nonzero offset time

    • Dynamically-sized input and output signals for an S-function with multiple input and output ports

  • The initial values of states, the sample time, and the size of input and output ports must be pre-selected value and cannot be passed as parameters to the s-function.

  • The value of sample time cannot be retrieved by any of the callback methods.

Note

S-functions with one input and one output port can have dynamically-sized signals.

Legacy Code Tool
  • Generates C MEX S-functions for existing functions written in C or C++ only. The tool does not support transformation of MATLAB or Fortran functions.

  • Can interface with C++ functions, but not C++ objects.

  • Does not support simulating continuous or discrete states.

  • Does not support use of function pointers as the output of the legacy function being called.

  • Always sets the S-function's flag for direct feedthrough (sizes.DirFeedthrough) to true.

  • Supports only the continuous, but fixed in minor time step, sample time and offset option.

  • Supports complex numbers, but only with Simulink built-in data types.

  • Does not support the following S-function features:

    • Work vectors, other than general DWork vectors

    • Frame-based input and output signals

    • Port-based sample times

    • Multiple block-based sample times