| Contents | Index |
| On this page… |
|---|
A MATLAB Function block allow you to use the MATLAB language to define custom functionality. These blocks are good starting point for creating a custom block if:
You have an existing MATLAB function that models the custom functionality.
You find it easier to model custom functionality using a MATLAB function than using the Simulink block diagram.
The custom functionality does not include continuous or discrete dynamic states.
You can create a custom block from a MATLAB function using one of the following types of MATLAB function blocks.
The Fcn block allows you to use a MATLAB expression to define a single-input, single-output (SISO) block.
The Interpreted MATLAB Function block allows you to use a MATLAB function to define a SISO block.
The MATLAB Function block allows you to define a custom block with multiple inputs and outputs that can be deployed to embedded processors.
Each of these blocks has advantages in particular modeling applications. For example, you can generate code from models containing MATLAB Function blocks while you cannot generate code for models containing a Fcn block.
Subsystem blocks allow you to build a Simulink diagram to define custom functionality. These blocks serve as a good starting point for creating a custom block if:
You have an existing Simulink diagram that models custom functionality.
You find it easier to model custom functionality using a graphical representation than using hand-written code.
The custom functionality is a function of continuous or discrete system states.
You can model the custom functionality using existing Simulink blocks.
Once you have a Simulink subsystem that models the required behavior, you can convert it into a custom block by:
Masking the block to hide the block contents and provide a custom block dialog.
Placing the block in a library to prohibit modifications and allow for easily updating copies of the block.
See Working with Block Libraries and Working with Block Masks in Using Simulink for more information.
S-function blocks allow you to write MATLAB, C, or C++ code to define custom functionality. These blocks serve as a good starting point for creating a custom block if:
You have existing MATLAB, C, or C++ code that models custom functionality.
You need to model continuous or discrete dynamic states or other system behaviors that require access to the S-function API.
You cannot model the custom functionality using existing Simulink blocks.
You can create a custom block from an S-function using one of the following types of S-function blocks.
The Level-2 MATLAB S-Function block allows you to write your S-function using the MATLAB language. (See Writing S-Functions in MATLAB). You can debug a MATLAB S-function during a simulation using the MATLAB debugger.
The S-Function block allows you to write your S-function in C or C++, or to incorporate existing code into your model using a C MEX wrapper. (See Writing S-Functions in C.)
The S-Function Builder block assists you in creating a new C MEX S-function or a wrapper function to incorporate legacy C or C++ code. (See Building S-Functions Automatically.)
The Legacy Code Tool transforms existing C or C++ functions into C MEX S-functions. (See Integrating Existing C Functions into Simulink Models with the Legacy Code Tool.)
The S-function target in the Simulink Coder product automatically generates a C MEX S-function from a graphical subsystem. If you want to build your custom block in a Simulink subsystem, but implement the final version of the block in an S-function, you can use the S-function target to convert the subsystem to an S-function. See Generated S-Function Block in the Simulink Coder User's Guide for details and limitations on using the S-function target.
MATLAB S-functions and MATLAB functions for code generation have some fundamental differences.
The Simulink Coder product can generate code for both MATLAB S-functions and MATLAB functions for code generation. However, MATLAB S-functions require a Target Language Compiler (TLC) file for code generation. MATLAB functions for code generation do not require a TLC-file.
MATLAB S-functions can use any MATLAB function while MATLAB functions for code generation are a subset of the MATLAB language. For a list of supported functions for code generation, see Functions Supported for Code Generation.
MATLAB S-functions can model discrete and continuous state dynamics while MATLAB functions for code generation cannot model state dynamics.
Each S-function block allows you to incorporate legacy code into your model, as follows.
A MATLAB S-function accesses legacy code through its TLC-file. Therefore, the legacy code is available only in the generated code, not during simulation.
A C MEX S-functions directly calls legacy C or C++ code.
The S-Function Builder generates a wrapper function that calls the legacy C or C++ code.
The Legacy Code Tool generates a C MEX S-function to call the legacy C or C++ code, which is optimized for embedded systems. See Integrating Existing C Functions into Simulink Models with the Legacy Code Tool for more information.
See Integration Options in the Simulink Coder User's Guide for more information.
See Example Using S-Functions to Incorporate Legacy C Code in "Writing S-Functions" for an example.
![]() | When to Create Custom Blocks | Comparison of Custom Block Functionality | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |