Modeling Patterns for C Code
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, seePrepare a Model for Code Generation.
Topics
Types, Operators, and Expressions
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.
Create a data type conversion using a Data Type Conversion block, Stateflow Chart, or MATLAB Function block.
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.
Perform bitwise operations using the Bitwise Operator block, a Stateflow Chart, or MATLAB Function block.
To generate an enumerated data type, define an enumeration class in a MATLAB file.
控制流
Use a Switch block, a Stateflow Chart, or MATLAB Function block to create anif-else
statement in the generated code.
使用一个开关块或MATLAB函数块to create answitch
statement in the generated code.
Use a For-Iterator Subsystem block, Stateflow Chart, or MATLAB Function block to create afor
loop in the generated code.
Use a White Iterator Subsystem block, Stateflow Chart, or MATLAB Function block to create awhile
loop in the generated code.
Use a While Iterator Subsystem block or Stateflow Chart to create ado while
loop 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.
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
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
Create a flat structure or nested structures that store parameter data in the generated code.
Create a flat structure that contains signal data in the generated code.
Create a structure whose fields are also structures.
Store Boolean data in bitfields in the generated code.
Arrays
Create an array of parameter data in the generated code.
Create an array of signal data in the generated code.