Skip to Main Content Skip to Search
Product Documentation

S-Function Examples

Overview of Examples

To run an example:

  1. In the MATLAB Command Window, enter sfundemos.mdl.

    The S-function demo 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.

MATLAB code

toolbox/simulink/simdemos/simfeatures

C, C++, and Fortran code

toolbox/simulink/simdemos/simfeatures/src

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.mmsfcndemo_sfundsc1.mdlImplement an S-function with an inherited sample time.
msfcn_limintm.mmsfcndemo_limintm.mdlImplement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.
msfcn_multirate.mmsfcndemo_multirate.mdlImplement a multirate system.
msfcn_times_two.mmsfcndemo_timestwo.mdlImplement an S-function that doubles its input.
msfcn_unit_delay.mmsfcndemo_sfundsc2.mdlImplement a unit delay.
msfcn_varpulse.mmsfcndemo_varpulse.mdlImplement 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.mmsfcndemo_vsfunc.mdlImplement 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 demo models.

FilenameDescription
csfunc.mDefine a continuous system in state-space format.
dsfunc.mDefine a discrete system in state-space format.
limintm.mImplement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.
mixedm.mImplement a hybrid system consisting of a continuous integrator in series with a unit delay.
sfun_varargm.mImplement an S-function that shows how to use the MATLAB command varargin.
simom.mImplement a system in state-space format with internal A, B, C, and D matrices, using the equations
dx/dt = Ax + Bu
y = Cx + Du
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 MATLAB S-function.
simom2.mImplement 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.
vdlmintm.mImplement a discrete limited integrator. This example is identical to vlimintm.m, except that the limited integrator is discrete.
vdpm.mImplement the Van der Pol equation (similar to the demo model, vdp).
vlimintm.mImplement a continuous limited integrator. This S-function illustrates how to build an S-function that can accommodate a dynamic input and state width.
vsfunc.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
barplot.csfcndemo_barplot.mdlAccess Simulink signals without using the standard block inputs.
csfunc.csfcndemo_csfunc.mdlImplement a continuous system.
dlimintc.cNo model availableImplement a discrete-time limited integrator.
dsfunc.csfcndemo_dsfunc.mdlImplement a discrete system.
limintc.cNo model availableImplement a limited integrator.
mixedm.csfcndemo_mixedm.mdlImplement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z).
mixedmex.csfcndemo_mixedmex.mdlImplement a hybrid dynamic system with a single output and two inputs.
quantize.csfcndemo_sfun_quantize.mdlImplement a vectorized quantizer. Quantizes the input into steps as specified by the quantization interval parameter, q.
sdotproduct.csfcndemo_sdotproduct.mdlCompute dot product (multiply-accumulate) of two real or complex vectors.
sfbuilder_bususage.csfbuilder_bususage.mdlAccess S-Function Builder with a bus input and output.
sftable2.cNo model availableImplement a two-dimensional table lookup.
sfun_atol.csfcndemo_sfun_atol.mdlSet different absolute tolerances for each continuous state.
sfun_cplx.csfcndemo_cplx.mdlAdd complex data for an S-function with one input port and one parameter.
sfun_directlook.cNo model availableImplement a direct 1-D lookup.
sfun_dtype_io.csfcndemo_dtype_io.mdlImplement an S-function that uses Simulink data types for inputs and outputs.
sfun_dtype_param.csfcndemo_dtype_param.mdlImplement an S-function that uses Simulink data types for parameters.
sfun_dynsize.csfcndemo_sfun_dynsize.mdlImplements dynamically-sized outputs .
sfun_errhdl.csfcndemo_sfun_errhdl.mdlCheck parameters using the mdlCheckParameters S-function routine.
sfun_fcncall.csfcndemo_sfun_fcncall.mdlExecute function-call subsystems on the first and second output elements.
sfun_frmad.csfcndemo_frame.mdlImplement a frame-based A/D converter.
sfun_frmda.csfcndemo_frame.mdlImplement a frame-based D/A converter.
sfun_frmdft.csfcndemo_frame.mdlImplement a multichannel frame-based Discrete-Fourier transformation (and its inverse).
sfun_frmunbuff.csfcndemo_frame.mdlImplement a frame-based unbuffer block.
sfun_multiport.csfcndemo_sfun_multiport.mdlConfigure multiple input and output ports.
sfun_manswitch.cNo model availableImplement a manual switch.
sfun_matadd.csfcndemo_matadd.mdlAdd matrices in an S-function with one input port, one output port, and one parameter.
sfun_multirate.csfcndemo_sfun_multirate.mdlDemonstrate how to specify port-based sample times.
sfun_port_constant.csfcndemo_port_constant.mdlDemonstrate how to specify constant port-based sample times.
sfun_port_triggered.csfcndemo_port_triggered.mdlDemonstrate how to use port-based sample times in a triggered subsystem.
sfun_runtime1.csfcndemo_runtime.mdlImplement run-time parameters for all tunable parameters.
sfun_runtime2.csfcndemo_runtime.mdlRegister individual run-time parameters.
sfun_runtime3.csfcndemo_runtime.mdlRegister dialog parameters as run-time parameters.
sfun_runtime4.csfcndemo_runtime.mdlImplement run-time parameters as a function of multiple dialog parameters.
sfun_simstate.csfcndemo_sfun_simstate.mdlDemonstrate the S-function API for saving and restoring the SimState.
sfun_zc.csfcndemo_sfun_zc.mdlDemonstrate 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.csfcndemo_sfun_zc_sat.mdlDemonstrate zero crossings with saturation.
sfunmem.csfcndemo_sfunmem.mdlImplement a one-integration-step delay and hold memory function.
simomex.csfcndemo_simomex.mdlImplement 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.csfcndemo_stspace.mdlImplement 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.csfcndemo_stvctf.mdlImplement 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.csfcndemo_stvdtf.mdlImplement 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.csfcndemo_stvmgain.mdlImplement a time-varying matrix gain.
table3.cNo model availableImplement a 3-D lookup table.
timestwo.csfcndemo_timestwo.mdlImplement a C MEX S-function that doubles its input.
vdlmintc.cNo model availableImplement a discrete-time vectorized limited integrator.
vdpmex.csfcndemo_vdpmex.mdlImplement the Van der Pol equation.
vlimintc.cNo model availableImplement a vectorized limited integrator.
vsfunc.csfcndemo_vsfunc.mdlIllustrate 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

Description

sfun_timestwo_for.Fsfcndemo_timestwo_for.mdlImplement a Level-1 Fortran S-function that represents the timestwo.c S-function.
sfun_atmos.c
sfun_atmos_sub.F
sfcndemo_atmos.mdlCalculate 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.cppsfcndemo_counter_cpp.mdlStore a C++ object in the pointers vector PWork.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS