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 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 throughsfundemos
. 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 fromsfundemos
:
In the MATLAB Command Window, enter
sfundemos
.The S-function example library opens.
Each block represents a category of S-function examples.
Double-click a category to display the examples that it includes. For example, clickC-files.
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 |
|
C, C++, and Fortran code |
|
Level-2 MATLAB S-Function Examples
The
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, seeWrite Level-2 MATLAB S-Functions.matlabroot
/toolbox/simulink/simdemos/simfeatures
Filename | Model Name | Description |
---|---|---|
msfcn_dsc.m |
msfcndemo_sfundsc1 |
实现一个n S-function with an inherited sample time. |
msfcn_limintm.m |
msfcndemo_limintm |
实现一个continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions. |
msfcn_multirate.m |
msfcndemo_multirate |
实现一个multirate system. |
msfcn_times_two.m |
msfcndemo_timestwo |
实现一个n S-function that doubles its input. |
msfcn_unit_delay.m |
msfcndemo_sfundsc2 |
实现一个unit delay. |
msfcn_varpulse.m |
msfcndemo_varpulse |
实现一个variable pulse width generator by callingset_param from within a Level-2 MATLAB S-function. Also demonstrates how to use custom set and get methods for the blockSimState . |
msfcn_vs.m |
msfcndemo_vsfunc |
实现一个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
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, seeC/C++ S-Function BasicsandBuild S-Functions Automatically Using S-Function Builder.matlabroot
/toolbox/simulink/simdemos/simfeatures/src
Filename | Model Name | Description |
---|---|---|
csfunc.c |
sfcndemo_csfunc |
实现一个continuous system. |
dlimintc.c |
No model available | 实现一个discrete-time limited integrator. |
dsfunc.c |
sfcndemo_dsfunc |
实现一个discrete system. |
limintc.c |
No model available | 实现一个limited integrator. |
mixedm.c |
sfcndemo_mixedm |
实现一个hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z). |
mixedmex.c |
sfcndemo_mixedmex |
实现一个hybrid dynamic system with a single output and two inputs. |
slexQuantizeSFcn.c |
sfcndemo_sfun_quantize |
实现一个vectorized quantizer. Quantizes the input into steps as specified by the quantization interval parameter,q . |
sdotproduct.c |
sfcndemo_sdotproduct |
Compute dot product (multiply-accumulate) of two real or complex vectors. |
sfbuilder_bususage.c |
sfbuilder_bususage |
Access S-Function Builder with a bus input and output. |
sfbuilder_movingAverage.c |
sfbuilder_movingAverage |
Implement simple time window moving average usingStartandTerminate. |
sftable2.c |
sfcndemo_sftable2 |
实现一个two-dimensional table lookup. |
sfun_atol.c |
sfcndemo_sfun_atol |
Set different absolute tolerances for each continuous state. |
sfun_cplx.c |
sfcndemo_cplx |
Add complex data for an S-function with one input port and one parameter. |
sfun_directlook.c |
No model available | 实现一个direct 1-D lookup. |
sfun_dtype_io.c |
sfcndemo_dtype_io |
实现一个n S-function that uses Simulink®data types for inputs and outputs. |
sfun_dtype_param.c |
sfcndemo_dtype_param |
实现一个n S-function that uses Simulink data types for parameters. |
sfun_dynsize.c |
sfcndemo_sfun_dynsize |
Implements dynamically-sized outputs . |
sfun_errhdl.c |
sfcndemo_sfun_errhdl |
Check parameters using themdlCheckParameters S-function routine. |
sfun_fcncall.c |
sfcndemo_sfun_fcncall |
Execute function-call subsystems on the first and second output elements. |
sfun_frmad.c |
sfcndemo_frame |
实现一个frame-based A/D converter. |
sfun_frmda.c |
sfcndemo_frame |
实现一个frame-based D/A converter. |
sfun_frmdft.c |
sfcndemo_frame |
实现一个multichannel frame-based Discrete-Fourier transformation (and its inverse). |
sfun_frmunbuff.c |
sfcndemo_frame |
实现一个frame-based unbuffer block. |
sfun_multiport.c |
sfcndemo_sfun_multiport |
Configure multiple input and output ports. |
sfun_manswitch.c |
No model available | 实现一个manual switch. |
sfun_matadd.c |
sfcndemo_matadd |
Add matrices in an S-function with one input port, one output port, and one parameter. |
sfun_multirate.c |
sfcndemo_sfun_multirate |
Demonstrate how to specify port-based sample times. |
sfun_port_constant.c |
sfcndemo_port_constant |
Demonstrate how to specify constant port-based sample times. |
sfun_port_triggered.c |
sfcndemo_port_triggered |
Demonstrate how to use port-based sample times in a triggered subsystem. |
sfun_runtime1.c |
sfcndemo_runtime |
Implement run-time parameters for all tunable parameters. |
sfun_runtime2.c |
sfcndemo_runtime |
Register individual run-time parameters. |
sfun_runtime3.c |
sfcndemo_runtime |
Register dialog parameters as run-time parameters. |
sfun_runtime4.c |
sfcndemo_runtime |
Implement run-time parameters as a function of multiple dialog parameters. |
sfun_zc.c |
sfcndemo_sfun_zc |
Demonstrate use of nonsampled zero crossings to implementabs(u) . This S-function is designed to be used with a variable-step solver. |
sfun_zc_sat.c |
sfcndemo_sfun_zc_sat |
Demonstrate zero crossings with saturation. |
sfun_zc_cstate_sat.c |
sfcndemo_sfun_zc_cstate_sat |
实现一个continuous integrator with saturation limits and zero-crossing detection. |
sfun_integrator_localsolver.c |
sfcndemo_sfun_localsolver |
Demonstrate a continuous integrator where the continuous states are solved using a separate local solver instead of that used by the model. |
sfun_angle_events.c |
sfcndemo_angle_events |
实现一个method for robust and efficient detection of a rotating body crossing specified angles. |
sfun_angle_events.c |
No model available | Demonstrate angle detection and incorporate Stateflow®to schedule function calls. |
sfunmem.c |
sfcndemo_sfunmem |
实现一个one-integration-step delay and hold memory function. |
simomex.c |
sfcndemo_simomex |
实现一个single-input, two-output state-space dynamic system described by the state-space equations: dx/dt = Ax + Bu y = Cx + Du x is the state vector,u is vector of inputs, andy is the vector of outputs. |
stspace.c |
sfcndemo_stspace |
实现一个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_stvctf |
实现一个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.c |
sfcndemo_stvdtf |
实现一个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.c |
sfcndemo_stvmgain |
实现一个time-varying matrix gain. |
table3.c |
No model available | 实现一个3-D lookup table. |
timestwo.c |
sfcndemo_timestwo |
实现一个C MEX S-function that doubles its input. |
vdlmintc.c |
No model available | 实现一个discrete-time vectorized limited integrator. |
vdpmex.c |
sfcndemo_vdpmex |
Implement the Van der Pol equation. |
vlimintc.c |
No model available | 实现一个vectorized limited integrator. |
vsfunc.c |
sfcndemo_vsfunc |
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. |
sfun_pwm.c |
sfcndemo_pwm |
Illustrate how to create a controllable sample time block. |
sfun_d2c |
sfcndemo_d2c |
Illustrate 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
folder (open). For more on Fortran S-functions, seeCreate Level-2 Fortran S-Functions.matlabroot
/toolbox/simulink/simdemos/simfeatures/src
Filename | Model Name | Description |
---|---|---|
sfun_atmos.c sfun_atmos_sub.F |
sfcndemo_atmos |
Calculate 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
folder (open). For more on C++ S-functions, seeC/C++ S-Function BasicsandBuild S-Functions Automatically Using S-Function Builder.matlabroot
/toolbox/simulink/simdemos/simfeatures/src
Filename | Model Name | Description |
---|---|---|
sfun_counter_cpp.cpp |
sfcndemo_counter_cpp |
Store a C++ object in the pointers vectorPWork . |
sfbuilder_permute.cpp |
sfbuilder_permutation |
实现体育rmutation by calling external C++ classes using Start and Terminate. |
sfbuilder_linfilt.cpp |
sfbuilder_upsampling |
Implement 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, seeCreate a Blockset Project.
Filename | Description |
---|---|
slexBlocksetDesignerExample |
Organize S-function artifacts into a single project using Blockset Designer. |
See Also
Level-2 MATLAB S-Function|S-Function Builder|S-Function|MATLAB Function