Main Content

Customize Code Organization and Format

Custom file processing (CFP) tools allow you to customize the organization and formatting of generated code. With these tools, you can:

  • Generate a source (.c or .cpp) or header (.h) file. If you configure the Code interface packaging for a model to be Reusable function or Nonreuseable function (not C++ class), you can use a custom file processing template to control how the code generator:

    • Emits code to standard generated files---model.c or .cpp and model.h

    • Produces files that are independent of model code

  • Organize generated code into sections (such as includes, typedefs, functions, and more). Your CFP template can emit code (for example, functions), directives (such as #define or #include statements), or comments into each section.

  • Generate custom file banners (comment sections) at the start and end of generated code files and custom function banners that precede functions in the generated code.

  • Generate code to call model functions, such as model_initialize, model_step, and so on.

  • Generate code to read and write model inputs and outputs.

  • Generate a main program module.

  • Obtain information about the model and the generated files from the model.

Custom File Processing Components

The custom file processing features are based on the following interrelated components:

  • Code generation template (CGT) files: a CGT file defines the top-level organization and formatting of generated code. See Code Generation Template (CGT) Files.

  • The code template API: a high-level Target Language Compiler (TLC) API that provides functions with which you can organize code into named sections and subsections of generated source and header files. The code template API also provides utilities that return information about generated files, generate standard model calls, and perform other functions. See Code Template API Summary.

  • Custom file processing (CFP) templates: a CFP template is a TLC file that manages the process of custom code generation. A CFP template assembles code to be generated into buffers. A CFP template also calls the code template API to emit the buffered code into specified sections of generated source and header files. A CFP template interacts with a CGT file, which defines the ordering of major sections of the generated code. See Custom File Processing (CFP) Templates.

To use CFP templates, you must understand TLC programming, for more information, see Target Language Compiler.

Custom File Processing Configuration

Customize generated code by specifying code and data templates on the Code Generation > Templates pane:

GoalAction
Specify a template that defines the top-level organization and formatting of generated source code (.c or .cpp) files Enter a code generation template (CGT) file for the Source file (*.c) template parameter.
Specify a template that defines the top-level organization and formatting of generated header (.h) files Enter a CGT file for the Header file (*.h) template parameter. This template file can be the same template file that you specify for Source file (.c) template. If you use the same template file, source and header files contain identical banners. The default template is matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt.
Specify a template that organizes generated code into sections (such as includes, typedefs, functions, and more)Enter a custom file processing (CFP) template file for the File customization template parameter. A CFP template can emit code, directives, or comments into each section. For more information, see Custom File Processing (CFP) Templates.
Generate a model-specific example main program moduleSelect Generate an example main program. For more information, see Generate Code for an Application.

Note

Place the template files that you specify on the MATLAB® path.