|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 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.
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 What Are Masks?.
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
The most common use of S-functions is to create custom Simulink blocks (see When to Create Custom Blocks). 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.