Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Building S-Functions Automatically

About 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.

      Note   This directory 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. (For more information, see Using the Bus Editor. Alternatively, you can use Simulink.Bus as follows.

    1. At the MATLAB command line, enter:

      a = Simulink.Bus

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

      a =
       
      Simulink.Bus
          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:

    open_system([matlabroot,'/toolbox/simulink/simdemos/
    simfeatures/sfbuilder_bususage.mdl']);
  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. If you have not already done so, configure the mex command to work on your system.

    To configure the mex command, type mex -setup at the MATLAB command prompt.

  8. 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 directory (see How the S-Function Builder Builds an S-Function).

  9. 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.

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

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.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS