Main Content

Modeling Patterns for C Code Constructs

Apply modeling patterns that generate specific C constructs

Several standard methods are available for setting up a model to generate specific C constructs in your generated code. Using blocks, S-functions, Stateflow charts, MATLAB Function blocks, data objects, and custom storage classes, you can generate C constructs such as: data types, structures, arrays, control flow constructs, functions, preprocessor directives, and pointers. To begin, see Prepare a Model for Code Generation. Also see Standards, Guidelines, and Block Usage.

Topics

Types, Operators, and Expressions

Control Flow

  • If-Else
    Use a Switch block, a Stateflow Chart, or MATLAB Function block to create an if-else statement in the generated code.
  • Switch
    Use a Switch Case block or MATLAB Function block to create an switch statement in the generated code.
  • For Loop
    Use a For-Iterator Subsystem block, Stateflow Chart, or MATLAB Function block to create a for loop in the generated code.
  • While Loop
    Use a White Iterator Subsystem block, Stateflow Chart, or MATLAB Function block to create a while loop in the generated code.
  • Do While Loop
    Use a While Iterator Subsystem block or Stateflow Chart to create a do while loop in the generated code.

Functions

  • Function Call
    To generate a function call, add a subsystem, which implements the operations that you want.
  • Function Prototyping
    Create a function call using graphical functions or function prototype control.
  • External C Functions
    Integrate legacy C functions in the generated code by either creating an S-function or making a call to an external C function.

Preprocessor Directives

Structures

Arrays

Pointers

  • Pointers
    Generate code that accesses data from your handwritten code through a pointer.