| Contents | Index |
| On this page… |
|---|
To incorporate a C MEX S-function or legacy Level-1 MATLAB S-function into a Simulink model, drag an S-Function block from the User-Defined Functions block library into the model. Then specify the name of the S-function in the S-function name field of the S-Function block's Block Parameters dialog box, as illustrated in the following figure.

In this example, the model contains an S-Function block that references an instance of the C MEX file for the S-function timestwo.c.
Note If the MATLAB path includes a C MEX file and a MATLAB file having the same name referenced by an S-Function block, the S-Function block uses the C MEX file. |
To incorporate a Level-2 MATLAB S-function into a model, drag a Level-2 MATLAB S-Function block from the User-Defined Functions library into the model. Specify the name of the S-function in the S-function name field.
The S-Function block S-function parameters and Level-2 MATLAB S-Function block Parameters fields allow you to specify parameter values to pass to the corresponding S-function. To use these fields, you must know the parameters the S-function requires and the order in which the function requires them. (If you do not know, consult the S-function's author, documentation, or source code.) Enter the parameters, separated by a comma, in the order required by the S-function. The parameter values can be constants, names of variables defined in the MATLAB or model workspace, or MATLAB expressions.
The following example illustrates usage of the Parameters field to enter user-defined parameters for a Level-2 MATLAB S-function.

The model in this example incorporates the sample S-function msfcn_limintm.m.
The msfcn_limintm.m S-function accepts three parameters: a lower bound, an upper bound, and an initial condition. The S-function outputs the time integral of the input signal if the time integral is between the lower and upper bounds, the lower bound if the time integral is less than the lower bound, and the upper bound if the time integral is greater than the upper bound. The dialog box in the example specifies a lower and upper bound and an initial condition of 2, 3, and 2.5, respectively. The scope shows the resulting output when the input is a sine wave of amplitude 1.
See Processing S-Function Parameters and Error Handling for information on how to access user-specified parameters in an S-function.
You can use the masking facility to create custom dialog boxes and icons for your S-Function blocks. Masked dialog boxes can make it easier to specify additional parameters for S-functions. For a discussion on masking, see Working with Block Masks in Using Simulink.
You can use S-functions for a variety of applications, including:
Creating new general purpose blocks
Adding blocks that represent hardware device drivers
Incorporating existing C code into a simulation (see Integrating Existing C Functions into Simulink Models with the Legacy Code Tool)
Describing a system as a set of mathematical equations
Using graphical animations (see the inverted pendulum demo, penddemo)
The most common use of S-functions is to create custom Simulink blocks (see Creating Custom Blocks in Using Simulink). When you use an S-function to create a general-purpose block, you can use it many times in a model, varying parameters with each instance of the block.
![]() | What Is an S-Function? | How S-Functions Work | ![]() |

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 |