Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Comparison of Custom Block Functionality

Custom Block Considerations

When creating a custom block, you may want to consider the following.

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 TypeSupports Multiple Inputs and OutputsModels State DynamicsSupports Various Data Attributes
SubsystemYes, including bus signals.Yes.Yes, including all data types, numeric types, and dimensions supported by the Simulink software. Also supports frame-based signals.
FcnNo. Must have a single vector input and scalar output.No.Supports only real scalar signals with a data type of double or single.
MATLAB FcnNo. 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 FunctionYes, 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-functionYes.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-functionYes, 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 TypeSpeed of Updating the DiagramSimulation OverheadCode Generation Support
SubsystemProportional 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.
FcnVery fast.Minimal, but these blocks also provide limited functionality.Natively supported.
MATLAB FcnFast.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 FunctionCan 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-functionCan 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-functionCan 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.

Modeling Requirements

Multiple Input and Output Ports

The following types of custom blocks support multiple input and output ports.

Custom Block TypeMultiple Input and Output Port Support
SubsystemSupports 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 FcnSupports 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 FunctionSupports 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.

State Behavior and the S-Function API

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 TypeS-Function API Support
SubsystemCommunicates 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 FunctionAll 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-functionAccesses 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-functionAccesses the complete set of S-function APIs.

Data Attribute Support

All custom block types support real scalar inputs and outputs with a data type of double.

Custom Block TypeData Attribute Support
SubsystemSupports any data type supported by the Simulink software, including fixed-point types. Also supports complex, 2-D, n-D, and frame-based signals.
FcnSupports 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 FcnSupports 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 FunctionSupports 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.

Speed and Code Generation Requirements

Updating the Simulink Diagram

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 TypeSpeed of Updating the Diagram
SubsystemThe 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 FcnDoes not incur greater update cost than other Simulink blocks.
Embedded MATLAB FunctionPerforms 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.

Simulation Overhead

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 TypeSimulation Overhead
SubsystemIf 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.
FcnHas the least simulation overhead. The block is tightly integrated with the Simulink engine and implements a rudimentary expression language that is efficiently interpreted.
MATLAB FcnHas 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 FunctionPerforms 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-functionIncurs 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-functionSimulates 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.

Code Generation

Not all custom block types support code generation.

Custom Block TypeCode Generation Support
SubsystemSupports code generation.
FcnSupports code generation.
MATLAB FcnDoes not support code generation.
Embedded MATLAB FunctionSupports 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-functionGenerates 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-functionSupports 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.

  


Related Products & Applications

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