Level-2 MATLAB® S-functions allow you to create blocks with many of the features and capabilities of Simulink® built-in blocks, including:
Multiple input and output ports
The ability to accept vector or matrix signals
Support for various signal attributes including data type, complexity, and signal frames
Ability to operate at multiple sample rates
A Level-2 MATLAB S-function consists of
setup routine to configure the basic properties of the
S-function, and a number of callback methods that the Simulink engine invokes at appropriate times during the simulation.
A basic annotated version of the template resides at
The template consists of a top-level
setup function and a set
of skeleton local functions, each of which corresponds to a particular callback
method. Each callback method performs a specific S-function task at a particular
point in the simulation. The engine invokes the local functions using function
handles defined in the
setup routine. See Level-2 MATLAB S-Function Callback Methods for a table of the
supported Level-2 MATLAB S-function
A more detailed Level-2 MATLAB
S-function template resides at
We recommend that you follow the structure and naming conventions of the templates when creating Level-2 MATLAB S-functions. This makes it easier for others to understand and maintain the MATLAB S-functions that you create. See Write Level-2 MATLAB S-Functions for information on creating Level-2 MATLAB S-functions.
Like a Level-2 MATLAB S-function, a MEX S-function consists of a set of callback methods that the Simulink engine invokes to perform various block-related tasks during a simulation. MEX S-functions can be implemented in C, C++, or Fortran. The engine directly invokes MEX S-function routines instead of using function handles as with MATLAB S-functions. Because the engine invokes the functions directly, MEX S-functions must follow standard naming conventions specified by the S-function API.
An annotated C MEX S-function template resides at
The template contains skeleton implementations of all the required and optional callback methods that a C MEX S-function can implement.
For a more basic version of the template see
Level-2 MATLAB and MEX S-functions each have advantages. The advantage of Level-2 MATLAB S-functions is speed of development. Developing Level-2 MATLAB S-functions avoids the time consuming compile-link-execute cycle required when developing in a compiled language. Level-2 MATLAB S-functions also have easier access to MATLAB toolbox functions and can utilize the MATLAB Editor/Debugger.
MEX S-functions are more appropriate for integrating legacy code into a Simulink model. For more complicated systems, MEX S-functions may simulate faster than MATLAB S-functions because the Level-2 MATLAB S-function calls the MATLAB execution engine for every callback method.
See Available S-Function Implementations for information on choosing the type of S-function best suited for your application.