This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Use S-Functions in Models


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.

Passing Parameters to S-Functions

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 msfcndemo_limintm 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 -5.0, 5.0, and 0, 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 Block Masks.

When to Use an S-Function

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 Integrate C Functions Using Legacy Code Tool)

  • Describing a system as a set of mathematical equations

  • Using graphical animations (see the inverted pendulum example, penddemo)

The most common use of S-functions is to create custom Simulink blocks (see Block Creation Basics). 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.

Was this topic helpful?