This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Build S-Functions Automatically

About Building S-Functions Automatically

The S-Function Builder is a Simulink® block that integrates C/C++ code to build 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.


For examples of using the S-Function Builder to build S-functions, see the C file S-functions subsystem of the S-function examples provided with the Simulink product. To display the examples, enter sfundemos at the MATLAB® command line (see S-Function Examples for more information).

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

  1. Set the MATLAB current folder to the folder in which you want to create the S-function.


    This folder must be on the MATLAB path.

  2. If you wish to connect a bus signal to the Input or Output port of the S-Function Builder, you must first create a bus object. You perform this task interactively using the Simulink Bus Editor (see Create Bus Objects with the Bus Editor. Alternatively, you can use Simulink.Bus as follows.

    1. In the MATLAB Command Window, enter:

      a = Simulink.Bus

      As a result, the HeaderFile for the bus defaults to the empty character vector:

      a =
          Description: ''
           HeaderFile: ''
             Elements: [0x1 double]
    2. If you wish to specify the header file for the bus, then at the MATLAB command line:

      a.Headerfile = 'Busdef.h'

      If you do not specify a header file, Simulink automatically generates Sfunctionname_bus.h

    For a demonstration on how to use the S-Function Builder with a bus, see the S-Function Builder with buses example by entering the following command at the MATLAB command line:

  3. Create a new Simulink model.

  4. Copy an instance of the S-Function Builder block from the User-Defined Functions library in the Library Browser into the new model.

  5. Double-click the block to open the S-Function Builder dialog box (see S-Function Builder Dialog Box).

  6. 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).

  7. Click Build on the S-Function Builder to start the build process.

    The S-Function Builder builds a MEX file that implements the specified S-function and saves the file in the current folder (see How the S-Function Builder Builds an S-Function).

  8. 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 folder 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.

Alternatively, you can deploy the generated S-function without using the S-Function Builder block or exposing the underlying C source file. To do this:

  1. Open the Simulink model that will include the S-function.

  2. Copy an S-Function block from the User-Defined Functions library in the Library Browser into the model.

  3. Double-click on the S-Function block.

  4. In the Block Parameters dialog box that opens, enter the name of the executable file generated by the S-Function Builder into the S-function name edit field.

  5. Enter any parameters needed by the S-function into the S-function parameters edit field. Enter the parameters in the order they appear in the S-Function Builder dialog box.

  6. Click OK on the S-Function Block Parameters dialog box.

You can use the generated executable file, for example, the .mexw32 file, in any S-Function block in any model as long as the executable file is on the MATLAB path.

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 folder:

  • sfun.c

    where sfun is the name of the S-function that you specify in the S-function name field of the S-Function Builder dialog box. This file contains the C source code representation of the standard portions of the generated S-function.

  • sfun_wrapper.c

    This file contains the custom code that you entered in the S-Function Builder dialog box.

  • sfun.tlc

    This file permits the generated S-function to run in Simulink Rapid Accelerator mode and allows for inlining the S-function during code generation. In addition, this file generates code for the S-function in Accelerator mode, thus allowing the model to run faster.

  • sfun_bus.h

    If you specify any Input port or Output port as a bus in the Data Properties pane of the S-Function Builder dialog box, but do not specify a header file, then the S-Function Builder automatically generates this header file.

After generating the S-function source code, the S-Function Builder uses the 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.