| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
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.
Note For examples of using the S-Function Builder to build S-functions, see the C-file S-functions subsystem of the S-function demos provided with the Simulink product. To display the demos, enter sfundemos at the MATLAB command line (see S-Function Examples for more information). |
To build an S-function with the S-Function Builder:
Set the MATLAB current directory to the directory in which you want to create the S-function.
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.
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]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']);
Copy an instance of the S-Function Builder block from the User-Defined Functions library in the Library Browser into the new model.

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

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).
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.
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).
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:
Open the Simulink model that will include the S-function.
Copy an S-Function block from the User-Defined Functions library in the Library Browser into the model.
Double-click on the S-Function block.
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.
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.
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.
The S-Function Builder builds an S-function as follows. First, it generates the following source files in the current directory:
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 Accelerator mode and to allow for inlining during code generation.
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.
![]() | Introduction | S-Function Builder Dialog Box | ![]() |

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 |