Main Content

Modeling Patterns for C Code

Apply modeling patterns that generate specific C constructs

有几种标准方法可用于设置模型以生成生成的代码中的特定C结构。使用块,S函数,状态流图,MATLAB功能块,数据对象和自定义存储类,您可以生成C构造,例如:数据类型,结构,阵列,控制流量构造,功能,预处理器指令和指针。开始,请参阅Prepare a Model for Code Generation.

Topics

类型,操作员和表达式

Typedef

Create data type aliases by generatingtypedefstatements.

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

使用数据类型转换块,状态流图或MATLAB功能块创建数据类型转换。

Type Qualifiers

Apply theconstvolatilekeywords 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

使用位算子块,状态流图或MATLAB功能块执行位操作。

Enumeration

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

控制流

If-Else

Use a Switch block, a Stateflow Chart, or MATLAB Function block to create anif-elsestatement in the generated code.

Switch

使用一个开关块或MATLAB函数块to create answitchstatement in the generated code.

For Loop

Use a For-Iterator Subsystem block, Stateflow Chart, or MATLAB Function block to create aforloop in the generated code.

While Loop

Use a White Iterator Subsystem block, Stateflow Chart, or MATLAB Function block to create awhileloop in the generated code.

在循环时做

使用一段时间迭代子系统块或状态流图表创建一个do whileloop in the generated code.

Functions

功能调用

To generate a function call, add a subsystem, which implements the operations that you want.

功能原型

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)

通过在模型中实现变体块,在生成的代码中包含预处理条件。

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.