Main Content

S-Function Examples

These examples show you how to work with a variety of S-functions or programs that use S-functions, including C/C++ S-functions, Fortran S-functions, S-function Builder, Level 2 MATLAB® S-functions, and Blockset Designer. Each section explains how to open the files and what is in them. These are good places to continue to develop an understanding of S-functions after learning the basics. These do not represent all the examples available for S-functions, but a relevant subsection.

Access S-Function Examples

Most of the S-function examples are run through sfundemos. This example library contains S-function models and is grouped by coding type, such as C S-functions or C++ S-functions.

To run an example from sfundemos:

  1. In the MATLAB Command Window, enter sfundemos.

    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.

MATLAB code

toolbox/simulink/sfuntemplates

C, C++, and Fortran code

toolbox/simulink/sfuntemplates/src

Level-2 MATLAB S-Function Examples

The matlabroot/toolbox/simulink/sfuntemplates folder (open) contains many Level-2 MATLAB S-functions. Consider starting off by looking at these files. For more on Level-2 MATLAB S-functions, see Write Level-2 MATLAB S-Functions.

FilenameModel NameDescription
msfcn_dsc.mmsfcndemo_sfundsc1Implement an S-function with an inherited sample time.
msfcn_limintm.mmsfcndemo_limintmImplement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.
msfcn_multirate.mmsfcndemo_multirateImplement a multirate system.
msfcn_times_two.mmsfcndemo_timestwoImplement an S-function that doubles its input.
msfcn_unit_delay.mmsfcndemo_sfundsc2Implement a unit delay.
msfcn_varpulse.mmsfcndemo_varpulseImplement a variable pulse width generator by calling the set_param function within a Level-2 MATLAB S-function. Also demonstrates how to use custom set and get methods for the block operating point.
msfcn_vs.mmsfcndemo_vsfuncImplement a variable sample time block 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/sfuntemplates/src folder (open) 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. For more on C S-functions, see C/C++ S-Function Basics and Build S-Functions Automatically Using S-Function Builder.

FilenameModel NameDescription
csfunc.csfcndemo_csfuncImplement a continuous system.
dlimintc.cNo model availableImplement a discrete-time limited integrator.
dsfunc.csfcndemo_dsfuncImplement a discrete system.
limintc.cNo model availableImplement a limited integrator.
mixedm.csfcndemo_mixedmImplement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z).
mixedmex.csfcndemo_mixedmexImplement a hybrid dynamic system with a single output and two inputs.
slexQuantizeSFcn.csfcndemo_sfun_quantizeImplement a vectorized quantizer. Quantizes the input into steps as specified by the quantization interval parameter, q.
sdotproduct.csfcndemo_sdotproductCompute dot product (multiply-accumulate) of two real or complex vectors.
sfbuilder_bususage.csfbuilder_bususageAccess S-Function Builder with a bus input and output.
sfbuilder_movingAverage.csfbuilder_movingAverageImplement simple time window moving average using Start and Terminate.
sftable2.csfcndemo_sftable2Implement a two-dimensional table lookup.
sfun_atol.csfcndemo_sfun_atolSet different absolute tolerances for each continuous state.
sfun_cplx.csfcndemo_cplxAdd 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_ioImplement an S-function that uses Simulink® data types for inputs and outputs.
sfun_dtype_param.csfcndemo_dtype_paramImplement an S-function that uses Simulink data types for parameters.
sfun_dynsize.csfcndemo_sfun_dynsizeImplements dynamically-sized outputs.
sfun_errhdl.csfcndemo_sfun_errhdlCheck parameters using the mdlCheckParameters S-function routine.
sfun_fcncall.csfcndemo_sfun_fcncallExecute function-call subsystems on the first and second output elements.
sfun_frmad.csfcndemo_frameImplement a frame-based A/D converter.
sfun_frmda.csfcndemo_frameImplement a frame-based D/A converter.
sfun_frmdft.csfcndemo_frameImplement a multichannel frame-based Discrete-Fourier transformation (and its inverse).
sfun_frmunbuff.csfcndemo_frameImplement a frame-based unbuffer block.
sfun_multiport.csfcndemo_sfun_multiportConfigure multiple input and output ports.
sfun_manswitch_mode.cNo model availableImplement a manual switch.
sfun_matadd.csfcndemo_mataddAdd matrices in an S-function with one input port, one output port, and one parameter.
sfun_multirate.csfcndemo_sfun_multirateDemonstrate how to specify port-based sample times.
sfun_port_constant.csfcndemo_port_constantDemonstrate how to specify constant port-based sample times.
sfun_port_triggered.csfcndemo_port_triggeredDemonstrate how to use port-based sample times in a triggered subsystem.
sfun_runtime1.csfcndemo_runtimeImplement run-time parameters for all tunable parameters.
sfun_runtime2.csfcndemo_runtimeRegister individual run-time parameters.
sfun_runtime3.csfcndemo_runtimeRegister dialog parameters as run-time parameters.
sfun_runtime4.csfcndemo_runtimeImplement run-time parameters as a function of multiple dialog parameters.
sfun_zc.csfcndemo_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.csfcndemo_sfun_zc_satDemonstrate zero crossings with saturation.
sfun_zc_cstate_sat.csfcndemo_sfun_zc_cstate_satImplement a continuous integrator with saturation limits and zero-crossing detection.
sfun_integrator_localsolver.csfcndemo_sfun_localsolverDemonstrate a continuous integrator where the continuous states are solved using a separate local solver instead of that used by the model.
sfun_angle_events.csfcndemo_angle_eventsImplement a method for robust and efficient detection of a rotating body crossing specified angles.
sfun_angle_events.cNo model availableDemonstrate angle detection and incorporate Stateflow® to schedule function calls.
sfunmem.csfcndemo_sfunmemImplement a one-integration-step delay and hold memory function.
simomex.c sfcndemo_simomex

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.

stspace.c sfcndemo_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.c sfcndemo_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.csfcndemo_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.csfcndemo_stvmgainImplement a time-varying matrix gain.
table3.cNo model availableImplement a 3-D lookup table.
timestwo.csfcndemo_timestwoImplement a C MEX S-function that doubles its input.
vdlmintc.cNo model availableImplement a discrete-time vectorized limited integrator.
vdpmex.csfcndemo_vdpmexImplement the Van der Pol equation.
vlimintc.c No model availableImplement a vectorized limited integrator.
vsfunc.csfcndemo_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.
sfun_pwm.csfcndemo_pwmIllustrate how to create a controllable sample time block.
sfun_d2csfcndemo_d2cIllustrate how to convert a discrete input signal into a smooth continuous output signal
sfun_pulse_delay.csfcndemo_vcoIllustrate how to implement an algorithm to efficiently delay a discrete valued input signal by a variable amount using variable sample time.

Fortran S-Function Examples

The following table lists sample Fortran® S-functions available in the matlabroot/toolbox/simulink/sfuntemplates/src folder (open). For more on Fortran S-functions, see Create Level-2 Fortran S-Functions.

FilenameModel Name

Description

sfun_atmos.c
sfun_atmos_sub.F
sfcndemo_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/sfuntemplates/src folder (open). For more on C++ S-functions, see C/C++ S-Function Basics and Build S-Functions Automatically Using S-Function Builder.

FilenameModel NameDescription
sfun_counter_cpp.cppsfcndemo_counter_cppStore a C++ object in the pointers vector PWork.
sfbuilder_permute.cppsfbuilder_permutationImplement permutation by calling external C++ classes using Start and Terminate.
sfbuilder_linfilt.cppsfbuilder_upsamplingImplement linear filtering with C++ STL algorithms for the upsampling of a sign wave.

Organizing S-Function in a Project

The following example shows how to arrange S-function artifacts into a project. This is accomplished using the Blockset Designer feature. For more on Blockset Designer, see Create a Blockset Project.

FilenameDescription
slexBlocksetDesignerExampleOrganize S-function artifacts into a single project using Blockset Designer.

See Also

| | |

Related Topics