This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

S-Function Examples

Overview of Examples

To run an example:

  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/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 (open) contains many Level-2 MATLAB S-functions. Consider starting off by looking at these files.

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 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_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 (open) 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.

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.
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 (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.

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.cNo model availableImplement 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.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_simstate.csfcndemo_sfun_simstateDemonstrate the S-function API for saving and restoring the SimState.
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.csf_angle_eventsDemonstrate 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

Fortran S-Function Examples

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

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/simdemos/simfeatures/src folder (open).

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 project can be shared with others as a zip archive or as a MATLAB toolbox.

FilenameDescription
slexSfunctionExamplesProjectOrganize S-function artifacts into a single project.

See Also

| | |

Related Topics