C 代码构造的建模模式
应用生成特定 C 构造的建模模式
可以通过几种标准方法设置模型,从而在生成的代码中生成特定的 C 构造。您可以使用模块、S-Function、Stateflow 图、MATLAB Function 模块、数据对象和自定义存储类来生成 C 构造,例如:数据类型、结构、数组、控制流构件、函数、预处理器指令和指针。有关入门信息,请参阅Prepare a Model for Code Generation。另请参阅标准、规范和模块用法。
主题
类型、运算符和表达式
- Typedef
通过生成typedef
语句来创建数据类型别名。 - 参数数据的定义、初始化和声明
控制参数数据的声明、定义和初始化的文件位置。 - 信号数据的定义和声明
控制信号数据的声明和定义的文件位置。 - Data Type Conversion
Create a data type conversion using a Data Type Conversion block, Stateflow Chart, or MATLAB Function block. - 类型限定符
将const
和volatile
关键字应用于一个表示参数数据的全局变量。 - 关系和逻辑运算符
使用 Simulink 模块、Stateflow 图和 MATLAB Function 模块实现关系和逻辑运算符。 - 按位运算
使用 Bitwise Operator 模块、Stateflow 图或 MATLAB Function 模块执行按位运算。 - 枚举
要生成枚举数据类型,请在 MATLAB 文件中定义枚举类。
控制流
- If-Else
使用 Switch 模块、Stateflow 图或 MATLAB Function 模块在生成的代码中创建一个if-else
语句。 - switch
使用 Switch Case 模块或 MATLAB Function 模块在生成的代码中创建一个switch
语句。 - for 循环
使用 For-Iterator Subsystem 模块、Stateflow 图或 MATLAB Function 模块在生成的代码中创建for
循环。 - While 循环
使用 White Iterator Subsystem 模块、Stateflow 图或 MATLAB Function 模块在生成的代码中创建while
循环。 - Do While 循环
使用 While Iterator Subsystem 模块或 Stateflow 图在生成代码中创建一个do while
循环。
函数
- 函数调用
要生成函数调用,请添加用于实现所需运算的子系统。 - 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.
预处理器指令
- 宏定义 (#define)
将参数数据生成为常量值宏。 - 条件包含 (#if / #endif)
通过在模型中实现变体模块,在生成的代码中包含预处理器条件句。
结构体
- 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. - 位域
将布尔数据存储在生成代码的位域中。
指针
- 指针
生成通过指针从手写代码中访问数据的代码。