|On this page…|
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 while you cannot generate code for models containing an Fcn block.
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 hand-written 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.
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 need to model 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. (SeeWrite 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 Generated S-Function Block in the Simulink Coder User's Guide for details and limitations on using the S-function target.
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 while 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 and C++ Code Generation — Alphabetical List.
MATLAB S-functions can model discrete and continuous state dynamics while 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-functions 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 Integration Options in the Simulink Coder User's Guide for more information.
See S-Functions Incorporate Legacy C Code in the Simulink Developing S-Functions for an example.