Differences Between MATLAB and C as Action Language Syntax
Stateflow® charts in Simulink® models have an action language property that defines the syntax for state and transition actions. The icon in the lower-left corner of the chart canvas indicates the action language for the chart:
MATLAB® is the action language.
C is the action language.
The action language you choose affects how you write state and transition actions, what operations are available, and how your charts generate code.
MATLAB is the default action language syntax for new Stateflow charts. To create a chart that uses C as the action language, enter:
sfnew -cChoosing an Action Language
Use MATLAB as the action language for:
Prototyping and rapid development, where you need to iterate quickly on algorithms and logic.
Matrix and vector operations, where your charts work extensively with arrays and complex mathematical operations.
MATLAB integration, where you want to leverage existing MATLAB functions and toolboxes.
Use C as the action language for:
Performance-critical applications where you need maximum execution speed for real-time systems.
Embedded targets and code generation for resource-constrained hardware.
Applications that contain deterministic behavior, where you need predictable timing and memory usage.
Differences in Action Languages
The MATLAB and C action languages differ in syntax, supported operations, and code generation behavior. These tables list the most significant differences between the two action languages.
Basic Operations
| Functionality | MATLAB as the Action Language | C as the Action Language |
|---|---|---|
Increment and decrement operations | The chart automatically corrects syntax to MATLAB syntax.
For example, | The chart supports the |
Assignment operations | The chart automatically corrects syntax to MATLAB syntax.
For example, | The chart supports the a += b, a –=
b, a *= b, and a /=
b supported. See Operations for Stateflow Data. |
Comparison operations | a ~= b | The chart supports the a != b and
!a operations. See Operations for Stateflow Data. |
Comment markers | Auto-correction to %. | The chart supports the // and /*
*/ supported. See Operations for Stateflow Data. |
| Conditional statements | You can use | Supported in state actions only. |
| Loop statements | You can use | Loop ststaments are not supported. For conditional and loop patterns, use graphical functions instead. See Reuse Logic Patterns by Defining Graphical Functions. |
Indexing and Data Handling
| Functionality | MATLAB as the Action Language | C as the Action Language |
|---|---|---|
Vector and matrix indexing | One-based indexing. For example, | Zero-based indexing. For example,
|
Index delimiters | Parenthesis with commas. For example,
| Brackets. For example, |
Variable-size data | You can modify variable-size chart data in state and transition actions. For more information, see Variable-Size Data in Charts That Use MATLAB as the Action Language. | You can modify variable-size chart data by using:
All computations with variable-size data must occur inside these functions, and not directly in states or transitions. For more information, see Variable-Size Data in Charts That Use C as the Action Language. |
| Scalar expansion | Not supported. | Supported. See Assign Values to All Elements. |
| String delimiters | Use double quotes
( | Use double ("...") or
single quotes ('...') as
delimiters. See Manage Textual Information by Using Strings. |
Complex data | Use the complex number notation | Use the |
Data type propagation | Follows MATLAB typing rules. For example, adding data of type
| Follows C typing rules. For example, adding data of type
|
Advanced Features and Code Generation
| Functionality | MATLAB as the Action Language | C as the Action Language |
|---|---|---|
Fixed-point constructs:
| Not supported. | Supported. See Override Fixed-Point Promotion in C Charts and Fixed-Point Context-Sensitive Constants. |
Explicit type cast operations | Use either the MATLAB type conversion function or the | Use either the MATLAB type conversion function or the |
Dot notation for specifying states, local data, message, and local events inside MATLAB functions | Supported. See Identify Data by Using Dot Notation. | Not supported. |
Structure parameters | Tunable and nontunable parameters are supported. | Only tunable parameters are supported. |
Use of global | Supported. | Not supported. |
Complex data | Use complex number notation | Use the |
Automatic Correction When Using MATLAB as the Action Language
Stateflow charts that use MATLAB as the action language automatically correct these C constructs to MATLAB syntax:
Increment and decrement operations such as
a++anda--. For example,a++is changed toa = a+1.Assignment operations such as
a += b,a –= b,a *= b, anda /= b. For example,a += bis changed toa = a+b.Evaluation operations such as
a != band!a. For example,a != bis changed toa ~= b.Comment markers
//and/* */are changed to%.
To disable this preference, use the sfpref
function:
sfpref(EnableLabelAutoCorrectionForMAL=false);
Guidelines for Using MATLAB as the Action Language
Follow these guidelines when coding in charts that use MATLAB as the action language.
Use one-based indexing for vectors and matrices
One-based indexing is consistent with MATLAB syntax. For more information, see Indexing Notation.
Use parentheses instead of brackets to index into vectors and matrices
This statement is valid:
a(2,5) = 0;
This statement is not valid:
a[2][5] = 0;
For more information, see Indexing Notation.
Use the MATLAB format for comments
Use % to specify comments in states and transitions for
consistency with MATLAB. For example, the following comment is valid:
% This is a valid comment in the style of MATLABC style comments, such as // and /* */,
are auto-corrected to use %.
Enclose transition actions with braces
This transition label contains a valid transition action:
E [x > 0] / {x = x+1;}This transition label is incorrect, but is auto-corrected to the valid syntax.
E [x > 0] / x = x+1;Do not use control flow logic in condition actions and transition actions
Control flow logic (such as if, switch,
for, and while statements) is
supported only in state actions. Use of control flow logic in condition actions
or transition actions, result in a syntax error.
Do not declare global or persistent variables in state actions
The keywords global and persistent are
not supported in state actions.
Assign an initial value to local and output data
When using MATLAB as the action language, data read without an initial value causes an error.
Include a type prefix for identifiers of enumerated values
The identifier TrafficColors.Red is valid, but
Red is not.
To generate code from your model, use MATLAB language features supported for code generation
Otherwise, use coder.extrinsic to call unsupported
functions, which gives the functionality that you want for simulation, but not
in the generated code. For a list of supported features and functions, see Language, Function, and Object Support (Simulink).