Documentation Center

  • Trial Software
  • Product Updates

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.
Interpreted MATLAB FunctionNo. Must have a single vector input and output.No.Supports only n-D, real, or complex signals with a data type of double.
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 MATLAB® 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.
Interpreted MATLAB FunctionFast.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.
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 MATLAB S-functionCan be slower if the S-function overrides methods executed when updating the diagram.Higher than for Interpreted MATLAB Function blocks because the MATLAB interpreter is called for every S-function method used. Very flexible, but very costly.MATLAB S-functions initialized as a SimViewingDevice do not generate code. Otherwise, MATLAB 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, Interpreted MATLAB FunctionSupports 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.
MATLAB FunctionSupports multiple input and output ports, including bus signals. See How Structure Inputs and Outputs Interface with Bus Signals for more information.
S-function (MATLAB 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 Integrate C Functions Using 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, Interpreted MATLAB Function, MATLAB FunctionAll create an mdlOutputs 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.
MATLAB 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 MATLAB 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.
Interpreted MATLAB FunctionSupports 2-D, n-D, and complex signals, but the signal must have a data type of double. Does not support frame-based signals.
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 (MATLAB 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, Interpreted MATLAB FunctionDoes not incur greater update cost than other Simulink blocks.
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, Simulink does not repeatedly update the block.
S-function (MATLAB 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 Simulink Engine Interaction 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 Capture 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.
Interpreted MATLAB FunctionHas 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 an Interpreted MATLAB Function 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 MATLAB is able to optimize it, or if the code uses the highly optimized MATLAB library functions.
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, a function for this block and a MATLAB function run at about the same speed. To further reduce the algorithm cost, you can disable debugging for all the MATLAB Function blocks in your model.

If the MATLAB Function block uses simulation-only capabilities to call out to the MATLAB interpreter, it incurs all the costs that a MATLAB S-function or Interpreted MATLAB Function block incur. Calling out to the MATLAB interpreter from a MATLAB Function block produces a warning to prevent you from doing so unintentionally.
MATLAB S-functionIncurs the same algorithm costs as the Interpreted MATLAB Function block, but with a slightly higher interface cost. Because MATLAB S-functions can handle multiple inputs and outputs, the packaging is more complicated than for the Interpreted MATLAB Function block. In addition, the Simulink engine calls the MATLAB interpreter for each block method you implement whereas for the Interpreted MATLAB Function block, it calls the MATLAB interpreter only for the mdlOutputs 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 with Simulink Coder™.

Custom Block TypeCode Generation Support
SubsystemSupports code generation.
FcnSupports code generation.
Interpreted MATLAB FunctionDoes not support code generation.
MATLAB FunctionSupports code generation. However, if your MATLAB Function block calls out to the MATLAB interpreter, it will build with the Simulink Coder product only if the calls to the MATLAB interpreter do not affect the block outputs. Under this condition, the Simulink Coder product omits these calls from the generated C code. This feature allows you to leave visualization code in place, even when generating embedded code.
MATLAB 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 MATLAB S-function is a SimViewingDevice, the Simulink Coder product automatically omits the block during code generation.
C MEX S-functionSupports code generation. For noninlined S-functions, the Simulink Coder 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 Insert S-Function Code in the Simulink Coder User's Guide for more information.

Was this topic helpful?