Choose Tools to Create Reusable Blocks in Simulink Using C/C++ or MATLAB
Simulink® provides you with an extensive suite of blocks and blocksets for modeling the components of your system. In addition to the standard built-in blocks, you can extend the modeling capability of Simulink using C/C++ and MATLAB® with block authoring tools to develop your own reusable blocks and blocksets. This topic helps you select the appropriate tool for creating your own reusable blocks and blocksets based on your modeling requirements.
The first section provides recommendations for block authoring tools that account for most of the common modeling requirements.
The second section provides more detailed information regarding additional considerations and recommendations for choosing block authoring tools for advanced configurations.
Choose Block Authoring Tools for Most Common Requirements
For most of your component modeling workflows using hand code, here are the three major considerations:
Use Blockset Designer for end-to-end management of your workflow.
Use MATLAB Function block to author components using MATLAB code.
Use S-Function Builder to author components using C/C++ code.
When you start authoring your blocks, it is encouraged to use Blockset Designer as your authoring tool. Blockset Designer enables you to collaborate, organize your blockset, and consolidate all authoring source files, build scripts, component dependencies, and documentation on one platform. It also allows you to deploy your blockset for reuse. For more information on using blockset designer, see Author Blocksets Using Blockset Designer.
You can author your blocks using MATLAB or C/C++ code within specialized APIs. Use MATLAB for access to all built-in MATLAB libraries and rapid prototyping. Use C/C++ if you have existing dependencies or for low-level control of your block algorithm.
If you decide to author your blocks using C/C++ code, use the S-Function Builder. This tool provides you with a graphical user interface and code editor to enable the modeling of you components. For more information on using S-Function Builder, see Author Blocks Using S-Function Builder.
If you decide to author your blocks using MATLAB code, use the MATLAB Function block. The block enables you to modify the output callback of a Simulink block to implement your block algorithms. For more information on using MATLAB Function block, see Author Blocks Using MATLAB Functions. Note that Blockset Designer does not have in-built support for MATLAB Function block but you can add it to a blockset project by including it inside a Simulink subsystem.
Considerations and Tools for Authoring Reusable Blocks with Advanced Configurations
This section provides you tools and considerations for authoring blocks with advanced configurations. Use this section to select a block authoring tool when the tool recommended in the previous section does not meet your modeling requirements.
The first subsection provides further considerations, such as using built-in Simulink blocks, before your start authoring your block.
The second subsection offers advanced decision-making support, such as code generation requirements and block customization, for authoring blocks using C/C++ code.
The third subsection offers advanced decision-making support, such as nature of required state dynamics, for authoring blocks using MATLAB code.
The final subsection combines all the three flowcharts from the previous subsections to provide complete decision making support for choosing a block authoring tool.
Further Considerations Before You Start Authoring Your Blocks
In Simulink you can model your components from ground up based on your component requirements or you can integrate existing implementation of your components from external tools or code into Simulink. For more information on integrating existing implementations, see Integrate Components from External Tools and Integrate External Code into Simulink.
In Simulink, you can model your components from ground up using built-in blocks or hand-code. Before you decide to model your component using hand-code, consider using the built-in Simulink blocks provided in Simulink block libraries and toolboxes to model your components as reusable subsystems. For more information, see Component-Based Modeling.
Use the block authoring tools mentioned here if the built-in blocks do not meet your modeling requirements or if you prefer to model your component using MATLAB or C/C++ code.
Author Blocks with Advanced Configurations Using C/C++ Code
You can model the components of your system using C/C++ code within specialized APIs. Simulink implements blocks written in C/C++ using an S-function. For more information about S-functions, see What Is an S-Function?. There are two tools that enable you to author blocks with handwritten C/C++ code: C MEX S-Function and S-Function Builder.
If you decide to author your component using C/C++ code, first consider whether you need to generate inline code without writing your own TLC. For more information about inlined S-function and writing a TLC, see Inlining S-Functions (Embedded Coder).
Use S-Function Builder if you want to generate inline code for your component without writing your own TLC. S-Function Builder is designed for ease of use, providing you with graphical user interface and editor to model your component. It implements a fixed set of Simulink callbacks to enable interaction with the Simulink engine and implementation of continuous and discrete state dynamics. S-Function Builder also generates a TLC as part of its build process, allowing you to generate inline code during code generation of a model containing the S-Function Builder without having to write a TLC yourself. For more information on using S-Function Builder, see Build S-Functions Automatically Using S-Function Builder.
Use C MEX S-Function if your component requires advanced configurations that are beyond the scope of the fixed set of Simulink callbacks provided in the S-Function Builder. C MEX S-Function enables you to create Simulink blocks that model complex dynamic systems by providing access to the comprehensive range of Simulink callbacks and advanced block customization options. This framework offers full flexibility for modeling complex and optimized components. For more information on creating a C MEX S-function, see Create and Implement a Basic C MEX S-Function. For C MEX S-function blocks, you need to manually write a TLC file if you want to generate inline code for a model containing the block. For more information on generating inline code for C MEX S-functions, see Write Fully Inlined S-Functions (Embedded Coder).
Author Blocks with Advanced Configurations Using MATLAB Code
You can model the components of your system using MATLAB code within specialized APIs. The three tools that enable you to author blocks using MATLAB are MATLAB Function, MATLAB System, and MATLAB S-Function blocks.
Use the MATLAB Function block for implementing block algorithms that do not require modeling of state dynamics. The MATLAB Function block allows you to modify the output callback of a Simulink block to implement algorithms without states. For more information on using MATLAB Function block, see Implement MATLAB Functions in Simulink with MATLAB Function Blocks.
Use the MATLAB System block to implement block algorithms that require modeling of discrete states. The MATLAB System blocks enable you to implement existing System Objects in Simulink. For more information on System Objects, see What Are System Objects?. MATLAB System block is optimized to model dynamic systems with streaming interface and discrete states. For more information on authoring MATLAB System blocks in Simulink, see Author Blocks Using MATLAB System Objects.
Use MATLAB S-Function to model components that implement continuous states or both continuous and discrete states. For more information, see Write Level-2 MATLAB S-Functions.
All of the Considerations and Tools for Authoring Reusable Blocks with Advanced Configurations
The chart below combines all the considerations and tools described in this section providing you with the complete decision making support for choosing the tools for modeling your component.