| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
Level-2 M-File S-Function Examples |
To run an example:
Enter sfundemos at the MATLAB command prompt.
The S-function demo library opens.

Each block represents a category of S-function examples.
Double-click a category to display the examples that it includes.

Double-click a block to open and run the example that it represents.
It might be helpful to examine some sample S-functions as you read the next chapters. Code for the examples is stored in the following subdirectories under the MATLAB root directory.
M-files | toolbox/simulink/simdemos/simfeatures |
C, C++, and Fortran | toolbox/simulink/simdemos/simfeatures/src |
The matlabroot/toolbox/simulink/simdemos/simfeatures directory contains many Level-2 M-file S-functions. Consider starting off by looking at these files.
| Filename | Model Name | Description |
|---|---|---|
| msfcn_dsc.m | msfcndemo_sfundsc1.mdl | Implement an S-function with an inherited sample time. |
| msfcn_limintm.m | msfcndemo_limintm.mdl | Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions. |
| msfcn_multirate.m | msfcndemo_multirate.mdl | Implement a multirate system. |
| msfcn_times_two.m | msfcndemo_timestwo.mdl | Implement an S-function that doubles its input. |
| msfcn_unit_delay.m | msfcndemo_sfundsc2.mdl | Implement a unit delay. |
| msfcn_varpulse.m | msfcndemo_varpulse.mdl | Implement a variable pulse width generator by calling set_param from within a Level-2 M-file S-function. Also demonstrates how to use custom set and get methods for the block SimState. |
| msfcn_vs.m | msfcndemo_vsfunc.mdl | Implement a variable sample time block in which the first input is delayed by an amount of time determined by the second input. |
The matlabroot/toolbox/simulink/simdemos/simfeatures directory also contains many Level-1 M-file S-functions, provided as reference for legacy models. Most of these Level-1 M-file S-functions do not have associated demo models.
Filename | Description |
|---|---|
Define a continuous system in state-space format. | |
Define a discrete system in state-space format. | |
Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions. | |
Implement a hybrid system consisting of a continuous integrator in series with a unit delay. | |
Implement an S-function that shows how to use the MATLAB command varargin. | |
Implement a system in state-space format with internal A, B, C, and D matrices, using the equations dx/dt
= Ax + By where x is the state vector, u is the input vector, and y is the output vector. The A, B, C, and D matrices are embedded in the M-file S-function. | |
Implement a system in state-space format with external A, B, C, and D matrices. The state-space structure is the same as in simom.m, but the A, B, C, and D matrices are provided externally as parameters to this S-function. | |
Implement a discrete limited integrator. This example is identical to vlimintm.m, except that the limited integrator is discrete. | |
Implement the Van der Pol equation (similar to the demo model, vdp). | |
Implement a continuous limited integrator. This S-function illustrates how to build an S-function that can accommodate a dynamic input and state width. | |
Illustrate how to create a variable sample time block. This S-function implements a variable step delay in which the first input is delayed by an amount of time determined by the second input. |
The matlabroot/toolbox/simulink/simdemos/simfeatures/src directory contains examples of C MEX S-functions, many of which have an M-file S-function counterpart. The C MEX S-functions are listed in the following table.
Filename | Model Name | Description |
|---|---|---|
| sfcndemo_barplot.mdl | Access Simulink signals without using the standard block inputs. | |
| sfcndemo_csfunc.mdl | Implement a continuous system. | |
| No model available | Implement a discrete-time limited integrator. | |
| sfcndemo_dsfunc.mdl | Implement a discrete system. | |
| No model available | Implement a limited integrator. | |
| sfcndemo_mixedm.mdl | Implement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z). | |
| sfcndemo_mixedmex.mdl | Implement a hybrid dynamic system with a single output and two inputs. | |
| sfcndemo_sfun_quantize.mdl | Implement a vectorized quantizer. Quantizes the input into steps as specified by the quantization interval parameter, q. | |
| sfcndemo_sdotproduct.mdl | Compute dot product (multiply-accumulate) of two real or complex vectors. | |
| sfbuilder_bususage.mdl | Access S-Function Builder with a bus input and output. | |
| No model available | Implement a two-dimensional table lookup. | |
| sfcndemo_sfun_atol.mdl | Set different absolute tolerances for each continuous state. | |
| sfcndemo_cplx.mdl | Add complex data for an S-function with one input port and one parameter. | |
| No model available | Implement a direct 1-D lookup. | |
| sfcndemo_dtype_io.mdl | Implement an S-function that uses Simulink data types for inputs and outputs. | |
| sfcndemo_dtype_param.mdl | Implement an S-function that uses Simulink data types for parameters. | |
| sfcndemo_sfun_dynsize.mdl | Implements dynamically-sized outputs . | |
| sfcndemo_sfun_errhdl.mdl | Check parameters using the mdlCheckParameters S-function routine. | |
| sfcndemo_sfun_fcncall.mdl | Execute function-call subsystems on the first and second output elements. | |
| sfcndemo_frame.mdl | Implement a frame-based A/D converter. | |
| sfcndemo_frame.mdl | Implement a frame-based D/A converter. | |
| sfcndemo_frame.mdl | Implement a multichannel frame-based Discrete-Fourier transformation (and its inverse). | |
| sfcndemo_frame.mdl | Implement a frame-based unbuffer block. | |
| sfcndemo_sfun_multiport.mdl | Configure multiple input and output ports. | |
| No model available | Implement a manual switch. | |
| sfcndemo_matadd.mdl | Add matrices in an S-function with one input port, one output port, and one parameter. | |
| sfcndemo_sfun_multirate.mdl | Demonstrate how to specify port-based sample times. | |
| sfun_port_constant.c | sfcndemo_port_constant.mdl | Demonstrate how to specify constant port-based sample times. |
| sfun_port_triggered.c | sfcndemo_port_triggered.mdl | Demonstrate how to use port-based sample times in a triggered subsystem. |
| sfcndemo_runtime.mdl | Implement run-time parameters for all tunable parameters. | |
| sfcndemo_runtime.mdl | Register individual run-time parameters. | |
| sfcndemo_runtime.mdl | Register dialog parameters as run-time parameters. | |
| sfcndemo_runtime.mdl | Implement run-time parameters as a function of multiple dialog parameters. | |
| sfcndemo_sfun_simstate.mdl | Demonstrate the S-function API for saving and restoring the SimState. | |
| sfcndemo_sfun_simstate.mdl | Demonstrate use of nonsampled zero crossings to implement abs(u). This S-function is designed to be used with a variable-step solver. | |
| sfcndemo_sfun_zc_sat.mdl | Demonstrate zero crossings with saturation. | |
| sfcndemo_sfunmem.mdl | Implement a one-integration-step delay and hold memory function. | |
| sfcndemo_simomex.mdl | Implement a single-input, two-output state-space dynamic system described by the state-space equations: dx/dt = Ax + Bu y = Cx + Du where x is the state vector, u is vector of inputs, and y is the vector of outputs. | |
| sfcndemo_stspace.mdl | Implement a set of state-space equations. You can turn this into a new block by using the S-Function block and mask facility. This example MEX-file performs the same function as the built-in State-Space block. This is an example of a MEX-file where the number of inputs, outputs, and states is dependent on the parameters passed in from the workspace. | |
| sfcndemo_stvctf.mdl | Implement a continuous-time transfer function whose transfer function polynomials are passed in via the input vector. This is useful for continuous time adaptive control applications. | |
| sfcndemo_stvdtf.mdl | Implement a discrete-time transfer function whose transfer function polynomials are passed in via the input vector. This is useful for discrete-time adaptive control applications. | |
| sfcndemo_stvmgain.mdl | Implement a time-varying matrix gain. | |
| No model available | Implement a 3-D lookup table. | |
| sfcndemo_timestwo.mdl | Implement a C MEX S-function that doubles its input. | |
| No model available | Implement a discrete-time vectorized limited integrator. | |
| sfcndemo_vdpmex.mdl | Implement the Van der Pol equation. | |
| No model available | Implement a vectorized limited integrator. | |
| sfcndemo_vsfunc.mdl | Illustrate how to create a variable sample time block. This block implements a variable-step delay in which the first input is delayed by an amount of time determined by the second input. |
The following table lists sample Fortran S-functions available in the matlabroot/toolbox/simulink/simdemos/simfeatures/src directory.
Filename | Model Name | Description |
|---|---|---|
| sfcndemo_timestwo_for.mdl | Implement a Level-1 Fortran S-function that represents the timestwo.c S-function. | |
| sfcndemo_atmos.mdl | Calculate the 1976 standard atmosphere to 86 km using a Fortran subroutine. | |
| No model available | Implement a Level-1 Fortran S-function that represents the simomex.c S-function. | |
| No model available | Implement a Level-1 Fortran S-function that represents the vdpmex.c S-function. |
The following table lists sample C++ S-functions available in the matlabroot/toolbox/simulink/simdemos/simfeatures/src directory.
Filename | Model Name | Description |
|---|---|---|
| sfcndemo_counter_cpp.mdl | Store a C++ object in the pointers vector PWork. |
![]() | S-Function Concepts | Selecting an S-Function Implementation | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |