| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
MATLAB function blocks allow you to use functions to define custom functionality. These blocks serve as a 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 via a MATLAB function than via a block diagram.
The custom functionality does not include continuous or discrete dynamic states.
You can create a custom block from an M-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 MATLAB Fcn block allows you to use a MATLAB function to define a SISO block.
The Embedded 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 Embedded 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 via a graphical representation than via hand-written code.
The custom functionality is a function of continuous or discrete system states.
The custom functionality can be modeled using existing Simulink blocks.
Once you have a Simulink subsystem that models the desired behavior, you can convert it into a custom block by:
Masking the block to hide the block's 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 M, 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 M, 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.
The custom functionality cannot be modeled 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 M-File S-Function block allows you to write your S-function in M. (See Writing S-Functions in M.) An M-file S-function can be debugged during 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 via 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 Real-Time Workshop 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 Creating Component Object Libraries and Enhancing Simulation Performance in the Real-Time Workshop User's Guide for details and limitations on using the S-function target.
M-file S-functions and Embedded MATLAB functions have some fundamental differences.
The Real-Time Workshop product can generate code for both M-file S-functions and Embedded MATLAB functions. However, M-file S-functions require a Target Language Compiler (TLC) file for code generation. Embedded MATLAB functions do not require a TLC-file.
M-file S-functions can use any MATLAB function. Embedded MATLAB functions support only a subset of the MATLAB functions. See Embedded MATLAB Function Library Reference in the Embedded MATLAB documentation for a list of supported functions.
M-file S-functions can model discrete and continuous state dynamics. Embedded MATLAB functions cannot model state dynamics.
Each S-function block allows you to incorporate legacy code into your model, as follows.
An M-file 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 Real-Time Workshop 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-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |