Writing S-Functions    

Building S-Functions Automatically

The S-Function Builder is a Simulink block that builds an S-function from specifications and C code that you supply. The S-Function Builder also serves as a wrapper for the generated S-function in models that use the S-function. This section explains how to use the S-Function Builder to build simple C MEX S-functions.

To build an S-function with the S-Function Builder:

  1. Set the MATLAB current directory to the directory in which you want to create the S-function.
  2. Create a new Simulink model.
  3. Copy an instance of the S-Function Builder block from the Simulink User-Defined Functions library into the new model.
  1. Double-click the block to open the S-Function Builder dialog box.

  2. Enter the name of the S-function in the S-function name field.
  3. If the S-function has parameters, enter default values for the parameters in the S-function parameters field.
  4. Use the specification and code entry panes on the S-Function Builder dialog box to enter information and custom source code required to tailor the generated S-function to your application (see S-Function Builder Dialog Box).
  5. If you have not already done so, configure the MATLAB mex command to work on your system.
  1. To configure the mex command, type mex -setup at the MATLAB command line.

  1. Click Build on the dialog box to start the build process.
  1. Simulink builds a MEX file that implements the specified S-function and saves the file in the current directory (see "How the S-Function Builder Builds an S-Function" on page 3-7).

  1. Save the model containing the S-Function Builder block.

Deploying the Generated S-Function

To use the generated S-function in another model, first check to ensure that the directory containing the generated S-function is on the MATLAB path. Then copy the S-Function Builder block from the model used to create the S-function into the target model and set its parameters, if necessary, to the values required by the target model.

How the S-Function Builder Builds an S-Function

The S-Function Builder builds an S-function as follows. First, it generates the following source files in the current directory:

After generating the S-function source code, the S-Function Builder uses the MATLAB mex command to build the MEX file representation of the S-function from the generated source code and any external custom source code and libraries that you specified.


  Writing S-Functions in C S-Function Builder Dialog Box