Types of Custom Blocks
MATLAB Function Blocks
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 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.
The Fcn block allows you to use a MATLAB expression to define a single-input, single-output (SISO) block.
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 a Fcn block.
MATLAB System Blocks
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
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.
For more information, see Custom Libraries and Author Block Masks.
C Caller Block
The C Caller block allows you to integrate C code into Simulink blocks. These blocks serve as a good starting point for creating a custom block if:
You have existing C code that models custom functionality.
Your C functions do not read or write global/static variables.
You want blocks to easily integrate with other Simulink features, such as Simulink Coverage™, Simulink Test™, and Simulink Coder™.
You are not modeling dynamic systems.
C Function Block
The C Function block allows you to integrate C code into Simulink blocks. These blocks serve as a good starting point for creating a custom block if:
You have existing C code that models custom functionality.
You want to call multiple C functions from the block, or call C functions conditionally.
You want to include C code to preprocess and postprocess data before and after calling C functions.
You want to specify different code for simulation and code generation.
You are not modeling dynamic systems with continuous states.
S-Function Blocks
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 S-Function block allows you to write your S-function in C or C++, or to incorporate existing code into your model using a C MEX wrapper. (See Author Blocks Using C MEX S-Functions.)
The S-Function Builder block assists you in creating a C MEX S-function or a wrapper function to incorporate legacy C or C++ code. (See Author Blocks Using C MEX S-Functions.)
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 Use S-Function Target for Model or Subsystem (Simulink Coder) in the Simulink Coder User's Guide for details and limitations on using the S-function target.
Masked Blocks
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 Author Block Masks.
See Also
Fcn | Interpreted MATLAB Function | C Caller | C Function | Level-2 MATLAB S-Function | MATLAB Function | MATLAB System | S-Function | S-Function Builder | Simulink Function | Subsystem