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 through
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
In the MATLAB Command Window, enter
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, click C-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.
C, C++, and Fortran code
Level-2 MATLAB S-Function Examples
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.
|Implement an S-function with an inherited sample time.|
|Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.|
|Implement a multirate system.|
|Implement an S-function that doubles its input.|
|Implement a unit delay.|
|Implement a variable pulse width generator by calling
|Implement 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
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.
|Implement a continuous system.|
|No model available||Implement a discrete-time limited integrator.|
|Implement a discrete system.|
|No model available||Implement a limited integrator.|
|Implement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z).|
|Implement a hybrid dynamic system with a single output and two inputs.|
|Implement a vectorized quantizer. Quantizes the input into steps
as specified by the quantization interval parameter,
|Compute dot product (multiply-accumulate) of two real or complex vectors.|
|Access S-Function Builder with a bus input and output.|
|Implement simple time window moving average using Start and Terminate.|
|Implement a two-dimensional table lookup.|
|Set different absolute tolerances for each continuous state.|
|Add complex data for an S-function with one input port and one parameter.|
|No model available||Implement a direct 1-D lookup.|
|Implement an S-function that uses Simulink® data types for inputs and outputs.|
|Implement an S-function that uses Simulink data types for parameters.|
|Implements dynamically-sized outputs .|
|Check parameters using the |
|Execute function-call subsystems on the first and second output elements.|
|Implement a frame-based A/D converter.|
|Implement a frame-based D/A converter.|
|Implement a multichannel frame-based Discrete-Fourier transformation (and its inverse).|
|Implement a frame-based unbuffer block.|
|Configure multiple input and output ports.|
|No model available||Implement a manual switch. For this file, change working
|Add matrices in an S-function with one input port, one output port, and one parameter.|
|Demonstrate how to specify port-based sample times.|
|Demonstrate how to specify constant port-based sample times.|
|Demonstrate how to use port-based sample times in a triggered subsystem.|
|Implement run-time parameters for all tunable parameters.|
|Register individual run-time parameters.|
|Register dialog parameters as run-time parameters.|
|Implement run-time parameters as a function of multiple dialog parameters.|
|Demonstrate use of nonsampled zero crossings to implement
|Demonstrate zero crossings with saturation.|
|Implement a continuous integrator with saturation limits and zero-crossing detection.|
|Demonstrate a continuous integrator where the continuous states are solved using a separate local solver instead of that used by the model.|
|Implement a method for robust and efficient detection of a rotating body crossing specified angles.|
|No model available||Demonstrate angle detection and incorporate Stateflow® to schedule function calls.|
|Implement a one-integration-step delay and hold memory function.|
Implement a single-input, two-output state-space dynamic system described by the state-space equations:
dx/dt = Ax + Bu y = Cx + Du
|Implement 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.|
|Implement 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.|
|Implement 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.|
|Implement a time-varying matrix gain.|
|No model available||Implement a 3-D lookup table.|
|Implement a C MEX S-function that doubles its input.|
|No model available||Implement a discrete-time vectorized limited integrator.|
|Implement the Van der Pol equation.|
|No model available||Implement a vectorized limited integrator.|
|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.|
|Illustrate how to create a controllable sample time block.|
|Illustrate how to convert a discrete input signal into a smooth continuous output signal|
|Illustrate 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
folder (open). For more on Fortran S-functions, see Create Level-2 Fortran S-Functions.
C++ S-Function Examples
The following table lists sample C++ S-functions available in the
folder (open). For more on C++ S-functions, see C/C++ S-Function Basics and
Build S-Functions Automatically Using S-Function Builder.
|Store a C++ object in the pointers vector
|Implement permutation by calling external C++ classes using Start and Terminate.|
|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, see Create a Blockset Project.
|Organize S-function artifacts into a single project using Blockset Designer.|