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
To build an S-function with the S-Function Builder:
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.
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 and Specify Bus Objects.
Alternatively, you can use
Simulink.Bus as follows.
In the MATLAB Command Window, enter:
a = Simulink.Bus
As a result, the
HeaderFile for the bus defaults to the empty
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
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:
Create a new Simulink model.
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).
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).
Save the model containing the S-Function Builder block.
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:
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
file, in any S-Function block in any model as long as the executable file is on the
The S-Function Builder builds an S-function as follows. First, it generates the following source files in the current folder:
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
This file contains the custom code that you entered in the S-Function Builder dialog box.
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.
If you specify any
Input port or
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