Create your own blocks to extend the built-in modeling functionality of Simulink®. For example, create new blocks to:
Integrate an existing code (legacy code) or algorithms into Simulink. Legacy code is your code that gives the functionality not available through existing Simulink library blocks. Simulink provides tools that let you integrate MATLAB, C/C++, and Fortran code. This method allows you to integrate System objects into Simulink.
Create a new Simulink block customized for your needs. You may want to create a custom block if the built-in Simulink library does not have a block that provides a necessary functionality.
Create a new custom functionality using a MATLAB® function rather than using a Simulink block diagram.
To summarize, integrate existing code if you already have code that you would like to access in Simulink. If you do not have existing code, but have a particular functionality that Simulink does not satisfy, create your own block.
A MATLAB Function block allows you to use the MATLAB language to define custom functionality. These blocks are 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 using a MATLAB function than using a 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 you can deploy to an embedded processor.
Each of these blocks has advantages in particular modeling applications. For example, you can generate code from models containing MATLAB Function blocks, whereas you cannot generate code for models containing an Fcn block.
A MATLAB System block allows you to use System objects written with the MATLAB language to define custom functionality. These blocks are a good starting point for creating a custom block if:
You have an existing System object™ that models the custom functionality.
You find it easier to model custom functionality using the MATLAB language than using a Simulink block diagram.
The custom functionality includes discrete dynamic states.
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 rather than using handwritten 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 box.
Placing the block in a library to prohibit modifications and allow for easily updating copies of the block.
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 use 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 Write Level-2 MATLAB S-Functions). You can debug a MATLAB S-function during a simulation using the MATLAB debugger.
The Legacy Code Tool transforms existing C or C++ functions into C MEX S-functions. (See Integrate C Functions Using 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 Accelerate Simulation, Reuse Code, or Protect Intellectual Property by Using S-Function Target (Simulink Coder) in the Simulink Coder User's Guide for details and limitations on using the S-function target.
You can customize any block by adding a mask to it. A mask is a custom interface to the block. You can customize a block using a mask in many ways, such as:
Change the block appearance.
Hide some or all of the parameters from the user of the block.
Customize block parameters.
To learn more about masked blocks, see Block Masks.
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 whereas MATLAB functions for code generation are a subset of the MATLAB language. For a list of supported functions for code generation, see Functions and Objects Supported for C/C++ Code Generation — Alphabetical List.
MATLAB S-functions can model discrete and continuous state dynamics whereas 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-function 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 Integrate C Functions Using Legacy Code Tool for more information.
See Call Reusable External Algorithm Code for Simulation and Code Generation (Simulink Coder) for more information.
See S-Functions Incorporate Legacy C Code in the Simulink Developing S-Functions for an example.