S-Function Examples

Overview of Examples

To run an example:

  1. In the MATLAB® Command Window, enter sfundemossfundemos.

    The S-function example library opens.

    Each block represents a category of S-function examples.

  2. Double-click a category to display the examples that it includes. For example, click C-files.

  3. 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 folder under the MATLAB root folder.



C, C++, and Fortran code


Level-2 MATLAB S-Function Examples

The matlabroot/toolbox/simulink/simdemos/simfeatures folder contains many Level-2 MATLAB S-functions. Consider starting off by looking at these files.

FilenameModel NameDescription
msfcn_dsc.mmsfcn_dsc.mmsfcndemo_sfundsc1msfcndemo_sfundsc1Implement an S-function with an inherited sample time.
msfcn_limintm.mmsfcn_limintm.mmsfcndemo_limintmmsfcndemo_limintmImplement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.
msfcn_multirate.mmsfcndemo_multiratemsfcndemo_multirateImplement a multirate system.
msfcn_times_two.mmsfcn_times_two.mmsfcndemo_timestwomsfcndemo_timestwoImplement an S-function that doubles its input.
msfcn_unit_delay.mmsfcn_unit_delay.mmsfcndemo_sfundsc2msfcndemo_sfundsc2Implement a unit delay.
msfcn_varpulse.mmsfcn_varpulse.mmsfcndemo_varpulsemsfcndemo_varpulseImplement a variable pulse width generator by calling set_param from within a Level-2 MATLAB S-function. Also demonstrates how to use custom set and get methods for the block SimState.
msfcn_vs.mmsfcn_vs.mmsfcndemo_vsfuncmsfcndemo_vsfuncImplement a variable sample time block in which the first input is delayed by an amount of time determined by the second input.

Level-1 MATLAB S-Function Examples

The matlabroot/toolbox/simulink/simdemos/simfeatures folder also contains many Level-1 MATLAB S-functions, provided as reference for legacy models. Most of these Level-1 MATLAB S-functions do not have associated example models.

csfunc.mcsfunc.mDefine a continuous system in state-space format.
dsfunc.mdsfunc.mDefine a discrete system in state-space format.
limintm.mlimintm.mImplement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.
mixedm.mmixedm.mImplement a hybrid system consisting of a continuous integrator in series with a unit delay.
sfun_varargm.msfun_varargm.mImplement an S-function that shows how to use the MATLAB command varargin.
vsfunc.mvsfunc.mIllustrate 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.

C S-Function Examples

The matlabroot/toolbox/simulink/simdemos/simfeatures/src folder contains examples of C MEX S-functions, many of which have a MATLAB S-function counterpart. The C MEX S-functions are listed in the following table.

FilenameModel NameDescription
csfunc.ccsfunc.csfcndemo_csfuncsfcndemo_csfuncImplement a continuous system.
dlimintc.cdlimintc.cNo model availableImplement a discrete-time limited integrator.
dsfunc.cdsfunc.csfcndemo_dsfuncsfcndemo_dsfuncImplement a discrete system.
limintc.climintc.cNo model availableImplement a limited integrator.
mixedm.cmixedm.csfcndemo_mixedmsfcndemo_mixedmImplement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z).
mixedmex.cmixedmex.csfcndemo_mixedmexsfcndemo_mixedmexImplement a hybrid dynamic system with a single output and two inputs.
slexQuantizeSFcn.cslexQuantizeSFcn.csfcndemo_sfun_quantizesfcndemo_sfun_quantizeImplement a vectorized quantizer. Quantizes the input into steps as specified by the quantization interval parameter, q.
sdotproduct.csdotproduct.csfcndemo_sdotproductsfcndemo_sdotproductCompute dot product (multiply-accumulate) of two real or complex vectors.
sfbuilder_bususage.csfbuilder_bususage.csfbuilder_bususagesfbuilder_bususageAccess S-Function Builder with a bus input and output.
sftable2.csftable2.cNo model availableImplement a two-dimensional table lookup.
sfun_atol.csfun_atol.csfcndemo_sfun_atolsfcndemo_sfun_atolSet different absolute tolerances for each continuous state.
sfun_cplx.csfun_cplx.csfcndemo_cplxsfcndemo_cplxAdd complex data for an S-function with one input port and one parameter.
sfun_directlook.csfun_directlook.cNo model availableImplement a direct 1-D lookup.
sfun_dtype_io.csfun_dtype_io.csfcndemo_dtype_iosfcndemo_dtype_ioImplement an S-function that uses Simulink® data types for inputs and outputs.
sfun_dtype_param.csfun_dtype_param.csfcndemo_dtype_paramsfcndemo_dtype_paramImplement an S-function that uses Simulink data types for parameters.
sfun_dynsize.csfun_dynsize.csfcndemo_sfun_dynsizesfcndemo_sfun_dynsizeImplements dynamically-sized outputs .
sfun_errhdl.csfun_errhdl.csfcndemo_sfun_errhdlsfcndemo_sfun_errhdlCheck parameters using the mdlCheckParameters S-function routine.
sfun_fcncall.csfun_fcncall.csfcndemo_sfun_fcncallsfcndemo_sfun_fcncallExecute function-call subsystems on the first and second output elements.
sfun_frmad.csfun_frmad.csfcndemo_framesfcndemo_frameImplement a frame-based A/D converter.
sfun_frmda.csfun_frmda.csfcndemo_framesfcndemo_frameImplement a frame-based D/A converter.
sfun_frmdft.csfun_frmdft.csfcndemo_framesfcndemo_frameImplement a multichannel frame-based Discrete-Fourier transformation (and its inverse).
sfun_frmunbuff.csfun_frmunbuff.csfcndemo_framesfcndemo_frameImplement a frame-based unbuffer block.
sfun_multiport.csfun_multiport.csfcndemo_sfun_multiportsfcndemo_sfun_multiportConfigure multiple input and output ports.
sfun_manswitch.csfun_manswitch.cNo model availableImplement a manual switch.
sfun_matadd.csfun_matadd.csfcndemo_mataddsfcndemo_mataddAdd matrices in an S-function with one input port, one output port, and one parameter.
sfun_multirate.csfun_multirate.csfcndemo_sfun_multiratesfcndemo_sfun_multirateDemonstrate how to specify port-based sample times.
sfun_port_constant.csfun_port_constant.csfcndemo_port_constantsfcndemo_port_constantDemonstrate how to specify constant port-based sample times.
sfun_port_triggered.csfun_port_triggered.csfcndemo_port_triggeredsfcndemo_port_triggeredDemonstrate how to use port-based sample times in a triggered subsystem.
sfun_runtime1.csfun_runtime1.csfcndemo_runtimesfcndemo_runtimeImplement run-time parameters for all tunable parameters.
sfun_runtime2.csfun_runtime2.csfcndemo_runtimesfcndemo_runtimeRegister individual run-time parameters.
sfun_runtime3.csfun_runtime3.csfcndemo_runtimesfcndemo_runtimeRegister dialog parameters as run-time parameters.
sfun_runtime4.csfun_runtime4.csfcndemo_runtimesfcndemo_runtimeImplement run-time parameters as a function of multiple dialog parameters.
sfun_simstate.csfun_simstate.csfcndemo_sfun_simstatesfcndemo_sfun_simstateDemonstrate the S-function API for saving and restoring the SimState.
sfun_zc.csfun_zc.csfcndemo_sfun_zcsfcndemo_sfun_zcDemonstrate use of nonsampled zero crossings to implement abs(u). This S-function is designed to be used with a variable-step solver.
sfun_zc_sat.csfun_zc_sat.csfcndemo_sfun_zc_satsfcndemo_sfun_zc_satDemonstrate zero crossings with saturation.
sfunmem.csfunmem.csfcndemo_sfunmemsfcndemo_sfunmemImplement a one-integration-step delay and hold memory function.
simomex.csimomex.csfcndemo_simomexsfcndemo_simomexImplement 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.
stspace.cstspace.csfcndemo_stspacesfcndemo_stspaceImplement 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.
stvctf.cstvctf.csfcndemo_stvctfsfcndemo_stvctfImplement 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.
stvdtf.cstvdtf.csfcndemo_stvdtfsfcndemo_stvdtfImplement 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.
stvmgain.cstvmgain.csfcndemo_stvmgainsfcndemo_stvmgainImplement a time-varying matrix gain.
table3.ctable3.cNo model availableImplement a 3-D lookup table.
timestwo.ctimestwo.csfcndemo_timestwosfcndemo_timestwoImplement a C MEX S-function that doubles its input.
vdlmintc.cvdlmintc.cNo model availableImplement a discrete-time vectorized limited integrator.
vdpmex.cvdpmex.csfcndemo_vdpmexsfcndemo_vdpmexImplement the Van der Pol equation.
vlimintc.cvlimintc.cNo model availableImplement a vectorized limited integrator.
vsfunc.cvsfunc.csfcndemo_vsfuncsfcndemo_vsfuncIllustrate 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.

Fortran S-Function Examples

The following table lists sample Fortran S-functions available in the matlabroot/toolbox/simulink/simdemos/simfeatures/src folder.

FilenameModel Name


sfun_timestwo_for.Fsfun_timestwo_for.Fsfcndemo_timestwo_forsfcndemo_timestwo_forImplement a Level-1 Fortran S-function that represents the timestwo.c S-function.
sfcndemo_atmossfcndemo_atmosCalculate the 1976 standard atmosphere to 86 km using a Fortran subroutine.

C++ S-Function Examples

The following table lists sample C++ S-functions available in the matlabroot/toolbox/simulink/simdemos/simfeatures/src folder.

FilenameModel NameDescription
sfun_counter_cpp.cppsfun_counter_cpp.cppsfcndemo_counter_cppsfcndemo_counter_cppStore a C++ object in the pointers vector PWork.

Was this topic helpful?