C 代码构造的建模模式
应用生成特定 C 构造的建模模式
可以通过几种标准方法设置模型,从而在生成的代码中生成特定的 C 构造。您可以使用模块、S-Function、Stateflow 图、MATLAB Function 模块、数据对象和自定义存储类来生成 C 构造,例如:数据类型、结构、数组、控制流构件、函数、预处理器指令和指针。有关入门信息,请参阅Prepare a Model for Code Generation。另请参阅标准、规范和模块用法。
主题
类型、运算符和表达式
- Typedef
Create data type aliases by generatingtypedef
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 theconst
andvolatile
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 Operator 模块、Stateflow 图或 MATLAB Function 模块执行按位运算。 - Enumeration
To generate an enumerated data type, define an enumeration class in a MATLAB file.
控制流
- 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)
将参数数据生成为常量值宏。 - Conditional Inclusions (#if / #endif)
Include preprocessor conditionals in your generated code by implementing variant blocks in your model.
结构体
- 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 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.
指针
- 指针
生成通过指针从手写代码中访问数据的代码。