| 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… |
|---|
When creating a custom block, you may want to consider the following.
Does the custom block need multiple input and output ports?
Does the block need to model continuous or discrete state behavior?
Will the block's inputs and outputs have various data attributes, such as data types or complexity?
How important is the affect of the custom block on the speed of updating the Simulink diagram or simulating the Simulink model?
Do you need to generate code for a model containing the custom block?
The following two tables provide an overview of how each custom block type addresses the previous questions. More detailed information for each consideration follows these two tables.
Modeling Requirements
| Custom Block Type | Supports Multiple Inputs and Outputs | Models State Dynamics | Supports Various Data Attributes |
|---|---|---|---|
| Subsystem | Yes, including bus signals. | Yes. | Yes, including all data types, numeric types, and dimensions supported by the Simulink software. Also supports frame-based signals. |
| Fcn | No. Must have a single vector input and scalar output. | No. | Supports only real scalar signals with a data type of double or single. |
| MATLAB Fcn | No. Must have a single vector input and output. | No. | Supports only n-D, real, or complex signals with a data type of double. |
| Embedded MATLAB Function | Yes, including bus signals. | No. | Yes, including all data types, numeric types, and dimensions supported by the Simulink software. Also supports frame-based signals. |
| Level-2 M-file S-function | Yes. | Yes, including limited access to other S-function APIs. | Yes, including all data types, numeric types, and dimensions supported by the Simulink software. Also supports frame-based signals. |
| C MEX S-function | Yes, including bus signals if using the Legacy Code Tool to generate the S-function. | Yes, including full access to all S-function APIs. | Yes, including all data types, numeric types, and dimensions supported by the Simulink software. Also supports frame-based signals. |
Speed and Code Generation Requirements
| Custom Block Type | Speed of Updating the Diagram | Simulation Overhead | Code Generation Support |
|---|---|---|---|
| Subsystem | Proportional to the complexity of the subsystem. For library blocks, can be slower the first time the library is loaded. | Proportional to the complexity of the subsystem. Library blocks introduce no additional overhead. | Natively supported. |
| Fcn | Very fast. | Minimal, but these blocks also provide limited functionality. | Natively supported. |
| MATLAB Fcn | Fast. | High and incurred when calling out to the MATLAB interpreter. These calls add overhead that should be avoided if simulation speed is a concern. | Not supported. |
| Embedded MATLAB Function | Can be slower if code must be generated to update the diagram. | Minimal if the MATLAB interpreter is not called. Simulation speed is equivalent to C MEX S-functions when the MATLAB interpreter is not called. | Natively supported, with exceptions. See Code Generation for more information. |
| Level-2 M-file S-function | Can be slower if the S-function overrides methods executed when updating the diagram. | Higher than for MATLAB Fcn blocks because the MATLAB interpreter is called for every S-function method used. Very flexible, but very costly. | M-file S-functions initialized as a SimViewingDevice do not generate code. Otherwise, M-file S-functions require a TLC-file for code generation. |
| C MEX S-function | Can be slower if the S-function overrides methods executed when updating the diagram. | Minimal, but proportional to the complexity of the algorithm and the efficiency of the code. | Might require a TLC-file. |
The following types of custom blocks support multiple input and output ports.
| Custom Block Type | Multiple Input and Output Port Support |
|---|---|
| Subsystem | Supports multiple input and output ports, including bus signals. In addition, you can modify the number of input and output ports based on user-defined parameters. See Self-Modifying Linked Subsystems for more information. |
| Fcn, MATLAB Fcn | Supports only a single input and a single output port. You must use a Mux block to combine the inputs and a Demux block to separate the outputs if you need to pass multiple signals into or out of these blocks. |
| Embedded MATLAB Function | Supports multiple input and output ports, including bus signals. See Working with Structures and Bus Signals for more information. |
| S-function (M-file or C MEX) | Supports multiple input and output ports. In addition, you can modify the number of input and output ports based on user-defined parameters. S-functions generated using the Legacy Code Tool also accept Simulink bus signals. See Integrating Existing C Functions into Simulink Models with the Legacy Code Tool for more information. |
Simulink blocks communicate with the Simulink engine through the S-function API, a set of methods that fully specifies the behavior of blocks. Each custom block type accesses a different sets of the S-function APIs, as follows.
| Custom Block Type | S-Function API Support |
|---|---|
| Subsystem | Communicates directly with the engine. You can model state behaviors using appropriate blocks from the Continuous and Discrete Simulink block libraries. |
| Fcn, MATLAB Fcn, Embedded MATLAB Function | All create an mdlOutput method to calculate the value of the outputs given the value of the inputs. You cannot access any other S-function API methods using one of these blocks and, therefore, cannot model state behavior. |
| M-file S-function | Accesses a larger subset of the S-function APIs, including the methods needed to model continuous and discrete states. For a list of supported methods, see Level-2 M-File S-Function Callback Methods in "Writing S-Functions". |
| C MEX S-function | Accesses the complete set of S-function APIs. |
All custom block types support real scalar inputs and outputs with a data type of double.
| Custom Block Type | Data Attribute Support |
|---|---|
| Subsystem | Supports any data type supported by the Simulink software, including fixed-point types. Also supports complex, 2-D, n-D, and frame-based signals. |
| Fcn | Supports only double or single data types. In addition, the input and output cannot be complex and the output must be a scalar signal. Does not support frame-based signals. |
| MATLAB Fcn | Supports 2-D, n-D, and complex signals, but the signal must have a data type of double. Does not support frame-based signals. |
| Embedded MATLAB Function | Supports any data type supported by the Simulink software, including fixed-point types. Also supports complex, 2-D, n-D, and frame-based signals. |
| S-function (M-file or C MEX) | Supports any data type supported by the Simulink software, including fixed-point types. Also supports complex, 2-D, n-D, and frame-based signals. |
The Simulink software updates the diagram before every simulation and whenever requested by the user. Every block introduces some overhead into the "update diagram" process.
| Custom Block Type | Speed of Updating the Diagram |
|---|---|
| Subsystem | The speed is proportional to the complexity of the algorithm implemented in the subsystem. If the subsystem is contained in a library, some cost is incurred when the Simulink software loads any unloaded libraries the first time the diagram is updated or readied for simulation. If all referenced library blocks remain unchanged, the Simulink software does not subsequently reload the library and compiling the model becomes faster than if the model did not use libraries. |
| Fcn, MATLAB Fcn | Does not incur greater update cost than other Simulink blocks. |
| Embedded MATLAB Function | Performs simulation through code generation, so these blocks might take a significant amount of time when first updated. However, because code generation is incremental, if the block and the signals connected to it have not changed, the Simulink software does not repeatedly update the block. |
| S-function (M-file or C MEX) | Incurs greater costs than other Simulink blocks only if it overrides methods executed when updating the diagram. If these methods become complex, they can contribute significantly to the time it takes to update the diagram. For a list of methods executed when updating the diagram, see the process view in How the Simulink Engine Interacts with C S-Functions. When updating the diagram, the Simulink software invokes all relevant methods in the model initialization phase up to, but not including, mdlStart. |
For most applications, any of the custom block types provide acceptable simulation performance. Use the Simulink profiler to obtain an indication of the actual performance. See Capturing Performance Data for more information.
You can break simulation performance into two categories. The interface cost is the time it takes to move data from the Simulink engine into the block. The algorithm cost is the time needed to perform the algorithm that the block implements.
| Custom Block Type | Simulation Overhead |
|---|---|
| Subsystem | If included in a library, introduces no interface or algorithm costs beyond what would normally be incurred if the block existed as a regular subsystem in the model. |
| Fcn | Has the least simulation overhead. The block is tightly integrated with the Simulink engine and implements a rudimentary expression language that is efficiently interpreted. |
| MATLAB Fcn | Has a higher interface cost than most blocks and the same algorithm
cost as a MATLAB function. When block data (such as inputs and outputs) is accessed or returned from a MATLAB Fcn block, the Simulink engine packages this data into MATLAB arrays. This packaging takes additional time and causes a temporary increase in memory during communication. If you pass large amounts of data across this interface, such as, frames or arrays, this overhead can be substantial. Once the data has been converted, the MATLAB interpreter executes the algorithm. As a result, the algorithm cost is the same as for MATLAB function. Efficient code can be competitive with C code if the MATLAB software is able to optimize it, or if the code uses the highly optimized MATLAB library functions. |
| Embedded MATLAB Function | Performs simulation through code generation and so incurs the
same interface cost as standard blocks. The algorithm cost of this block is harder to analyze because of the block's implementation. On average, an Embedded MATLAB function and a MATLAB function run at about the same speed. To further reduce the algorithm cost, you can disable debugging for all the Embedded MATLAB Function blocks in your model. If the Embedded MATLAB function uses simulation-only capabilities to call out to the MATLAB interpreter, it incurs all the costs that an M-file S-function or MATLAB Fcn block incur. Calling out to the MATLAB interpreter from an Embedded MATLAB function produces a warning to prevent you from doing so unintentionally. |
| M-file S-function | Incurs the same algorithm costs as the MATLAB Fcn block, but with a slightly higher interface cost. Because M-file S-functions can handle multiple inputs and outputs, the packaging is more complicated than for the MATLAB Fcn block. In addition, the Simulink engine calls the MATLAB interpreter for each block method you implement whereas for the MATLAB Fcn block, it calls the MATLAB interpreter only for the mdlOutput method. |
| C MEX S-function | Simulates via the compiled code and so incurs the same interface cost as standard blocks. The algorithm cost depends on the complexity of the S-function. |
Not all custom block types support code generation.
| Custom Block Type | Code Generation Support |
|---|---|
| Subsystem | Supports code generation. |
| Fcn | Supports code generation. |
| MATLAB Fcn | Does not support code generation. |
| Embedded MATLAB Function | Supports code generation. However, if your Embedded MATLAB Function block calls out to the MATLAB interpreter, it will build with the Real-Time Workshop product only if the calls to the MATLAB interpreter do not affect the block's outputs. Under this condition, the Real-Time Workshop product omits these calls from the generated C code. This feature allows you to leave visualization code in place, even when generating embedded code. |
| M-file S-function | Generates code only if you implement the algorithm using a Target Language Compiler (TLC) function. In accelerated and external mode simulations, you can choose to execute the S-function in interpretive mode by calling back to the MATLAB interpreter without implementing the algorithm in TLC. If the M-file S-function is a SimViewingDevice, the Real-Time Workshop product automatically omits the block during code generation. |
| C MEX S-function | Supports code generation. For noninlined S-functions, the Real-Time Workshop product uses the C MEX function during code generation. However, you must write a TLC-file for the S-function if you need to either inline the S-function or create a wrapper for hand-written code. See Integrating External Code With Generated C and C++ Code in the Real-Time Workshop User's Guide for more information. |
![]() | Types of Custom Blocks | Expanding 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 |