Stateflow^{®} charts in Simulink^{®} models have an action language property that defines the operations that you can use in state and transition actions. The language properties are:
MATLAB^{®} as the action language.
C as the action language.
For more information, see Differences Between MATLAB and C as Action Language Syntax.
This table summarizes the interpretation of all binary operations in Stateflow charts according to their order of precedence (0 = highest, 10 = lowest). Binary operations are left associative so that, in any expression, operators with the same precedence are evaluated from left to right. The order of evaluation for other operations is unspecified. For example, in this assignment
A = f() > g();
f()
and g()
is unspecified. For more predictable
results, it is good coding practice to split expressions that depend on the order of
evaluation into multiple statements.
Operation  Precedence  MATLAB as the Action Language  C as the Action Language 

0  Power.  Power. Enable this operation by clearing the Enable Cbit operations chart property. See Specify Properties for Stateflow Charts.  
1  Multiplication.  Multiplication.  
1  Division.  Division.  
1  Remainder. Noninteger operands are first cast to integers.  
2  Addition.  Addition.  
2  Subtraction.  Subtraction.  
3  Not supported. Use the  Shift  
3  Not supported. Use the  Shift  
4  Comparison, greater than.  Comparison, greater than.  
 4  Comparison, less than.  Comparison, less than. 
 4  Comparison, greater than or equal to.  Comparison, greater than or equal to. 
 4  Comparison, less than or equal to.  Comparison, less than or equal to. 
 5  Comparison, equal to.  Comparison, equal to. 
 5  Comparison, not equal to.  Comparison, not equal to. 
 5  Not supported. Use the operation  Comparison, not equal to. 
 5  Not supported. Use the operation  Comparison, not equal to. 
6  Logical AND. For bitwise AND, use the 
 
7  Not supported. For bitwise XOR, use the  Bitwise XOR (default). Noninteger operands are first cast to integers. Enable this operation by selecting the Enable Cbit operations chart property. See Specify Properties for Stateflow Charts.  
8  Logical OR. For bitwise OR, use the 
 
9  Logical AND.  Logical AND.  
10  Logical OR.  Logical OR. 
This table summarizes the interpretation of all unary operations and actions in Stateflow charts. Unary operations:
Have higher precedence than the binary operators.
Are right associative so that, in any expression, they are evaluated from right to left.
Operation  MATLAB as the Action Language  C as the Action Language 

 Logical NOT. For bitwise NOT, use the 

 Not supported. Use the operation  Logical NOT. 
 Negative.  Negative. 
 Not supported. Use the expression  Increment. Equivalent to 
 Not supported. Use the expression  Decrement. Equivalent to 
This table summarizes the interpretation of assignment operations in Stateflow charts.
Operation  MATLAB as the Action Language  C as the Action Language 

 Simple assignment.  Simple assignment. 
 Not supported. Use type cast operations to override fixedpoint promotion rules. See Type Cast Operations.  Assignment of fixedpoint numbers. See Override FixedPoint Promotion in C Charts. 
 Not supported. Use the expression  Equivalent to 
 Not supported. Use the expression  Equivalent to 
 Not supported. Use the expression  Equivalent to 
 Not supported. Use the expression  Equivalent to 
 Not supported. Use the expression  Equivalent to 
 Not supported. Use the expression  Equivalent to 
 Not supported. Use the expression  Equivalent to 
This table summarizes the interpretation of pointer and address operations in Stateflow charts.
Operation  MATLAB as the Action Language  C as the Action Language 

 Not supported.  Address operation. Use with custom code and Stateflow variables. 
 Not supported.  Pointer operation. Use only with custom code variables. 
For example, the model sf_bus_demo contains a custom C function that takes pointers as arguments. When
the chart calls the custom code function, it uses the &
operation to
pass the Stateflow data by address. For more information, see Integrate Custom Structures in Stateflow Charts.
To convert a value of one type to a value of another type, use type cast operations. You can cast data to an explicit type or to the type of another variable.
To cast a numeric expression to an explicit data type, use a MATLAB type conversion function of the form:
<type_fun>(expression)
<type_fun>
is a type conversion function that can be
double
, single
, int32
, int16
, int8
, uint32
, uint16
, or uint8
. In charts that use C as the action language,
<type_fun>
can also be
boolean
.
For example, this statement casts the expression x+3
to a 16bit
unsigned integer and assigns its value to the data y
:
y = uint16(x+3)
Alternatively, in charts that use MATLAB as the action language, you can use the cast
function with a type keyword
<type_key>
:
cast(expression,<type_key>)
Type keywords include 'double'
, 'single'
,
'int32'
, 'int16'
, 'int8'
,
'uint32'
, 'uint16'
, and
'uint8'
. For instance, as in the preceding example, this statement
casts the expression x+3
to a 16bit unsigned integer and assigns it to
y
:
y = cast(x+3,'uint16')
To make type casting easier, you can convert the type of a numeric expression based on the types of other data.
In charts that use MATLAB as the action language, call the cast
function with the
keyword 'like'
. For example, this statement converts the value of
x+3
to the same type as that of data z
and assigns
it to y
:
y = cast(x+3,'like',z)
z
can have any acceptable Stateflow type.In charts that use C as the action language, the type
operator
returns the type of an existing Stateflow data. Use this return value in place of an explicit type in a
cast
operation. For example, this statement converts the value of
x+3
to the same type as that of data z
and assigns
it to y
:
cast(x+3,type(z))
If you have Embedded Coder^{®} or Simulink Coder™, you can configure the code generator to apply a code replacement library (CRL) during code generation. The code generator changes the code that it generates for operations to meet application requirements. With Embedded Coder, you can develop and apply custom code replacement libraries.
Operation entries of the code replacement library can specify integral or fixedpoint operand and result patterns. You can use operation entries for these operations:
Addition +
Subtraction 
Multiplication *
Division /
For example, in this expression, you can replace the addition operator
+
with a targetspecific implementation if u1
,
u2
, and y
have types that permit a match with an
addition entry in the code replacement
library:
y = u1+u2
C chart semantics limit operator entry matching because the chart uses the target integer size as its intermediate type in arithmetic expressions. For example, this arithmetic expression computes the intermediate addition into a target integer:
y = (u1 + u2) % 3
For more information about using code replacement libraries that MathWorks^{®} provides, see What Is Code Replacement? (Simulink Coder) and Code Replacement Libraries (Simulink Coder). For information about developing custom code replacement libraries, see What Is Code Replacement Customization? (Embedded Coder) and Code You Can Replace From Simulink Models (Embedded Coder).