| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop |
| Contents | Index |
| Learn more about Real-Time Workshop |
The Custom Code library contains blocks that enable you to insert your own C or C++ code into specific functions within code generated by the Real-Time Workshop product for root models and subsystems. These blocks are a superset of code customization capabilities built into the Custom Code Configuration Parameters dialog box, and provide greater flexibility in terms of code placement than the controls on the dialog box.
The Custom Code library is part of the Real-Time Workshop library. You can access the Real-Time Workshop library by using the Simulink Library Browser. You can access Custom Code blocks by using the Real-Time Workshop library or by entering the MATLAB command rtwlib and then double-clicking the Custom Code Library block within it. Alternatively, you can enter the command custcode.

This chapter discusses use of the Custom Code library only.
Note If you need to integrate custom C++ code with generated C code or vice versa, see About External Code Integration for information on language compatibility requirements. |
All Custom Code blocks except for Model Header and Model Source can be dragged into either root models or atomic subsystems. Model Header and Model Source blocks can only be placed in root models.
Note You can use models containing Custom Code blocks as submodels (models referenced by Model blocks). However, when simulation targets for submodels are generated, all Custom Code blocks within them are ignored. On the other hand, when submodel code is generated to create Real-Time Workshop targets, custom code is included and is compiled in the generated code. |
The Custom Code library contains ten blocks that insert custom code into the generated model files and functions. You can view the blocks either by
Expanding the Custom Code node (under Real-Time Workshop library) in the Simulink Library Browser
Right-clicking the Custom Code sublibrary icon in the right pane of the Simulink Library Browser
The latter method opens the window shown in the previous section.
The two blocks on the top row contain text fields for inserting custom code at the top and bottom of
model.h — Model Header File block
model.c or model.cpp — Model Source File block
Each block contains two fields, in which you type or paste code and comments:
Top of Model Source/Header
Bottom of Model Source/Header
The next figure shows the Model Source block dialog box.

The eight function blocks in the second and third rows contain text fields to insert custom code sections at the top and bottom of these designated model functions:
SystemStart — System Start function block
SystemInitialize — System Initialize function block
SystemTerminate — System Terminate function block
SystemEnable — System Enable function block
SystemDisable — System Disable function block
SystemOutputs — System Outputs function block
SystemUpdate — System Update function block
SystemDerivatives — System Derivatives function block
Each of these blocks provides a System Outputs Function Custom Code dialog box that contains three fields:

![]() | Integrating External Code Using Custom Code Blocks | Example: Using a Custom Code Block | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |