Main Content

Modeling Patterns for C Code

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.

Topics

Types, Operators, and Expressions

Typedef

Create data type aliases by generating typedef statements.

Definition, Initialization, and Declaration of Parameter Data

Control the file placement of the declaration, definition, and initialization of parameter data.

Definition and Declaration of Signal Data

Control the file placement of the declaration and definition of signal data.

Data Type Conversion

Create a data type conversion using a Data Type Conversion block, Stateflow Chart, or MATLAB Function block.

Type Qualifiers

Apply the const and volatile keywords to a global variable that represents parameter data.

Relational and Logical Operators

Implement relational and logical operators using Simulink blocks, Stateflow Charts, and MATLAB Function blocks.

Bitwise Operations

Perform bitwise operations using the Bitwise Operator block, a Stateflow Chart, or MATLAB Function block.

Enumeration

To generate an enumerated data type, define an enumeration class in a MATLAB file.

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

Macro Definitions (#define)

Generate parameter data as a constant-valued macro.

Conditional Inclusions (#if / #endif)

Include preprocessor conditionals in your generated code by implementing variant blocks in your model.

Structures

Structures of Parameters

Create a flat structure or nested structures that store parameter data in the generated code.

Structures of Signals

Create a flat structure that contains signal data in the generated code.

Nested Structures of Signals

Create a structure whose fields are also structures.

Bitfields

Store Boolean data in bitfields in the generated code.

Arrays

Arrays for Parameters

Create an array of parameter data in the generated code.

Arrays for Signals

Create an array of signal data in the generated code.

Pointers

Pointers

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