Using Operations in Actions

Binary and Bitwise Operations

The table below summarizes the interpretation of all binary operators inStateflow® action language. These operators work with the following order of precedence (1 = highest, 10 = lowest). Binary operators evaluate from left to right.

You can specify that the binary operators &, ^, |, &&, and || are interpreted as bitwise operators in Stateflow generated C code for a chart or for all the charts in a model. See these individual operators in the table below for specific binary or bitwise operator interpretations.

Example

Precedence

Description

a * b

1

Multiplication

a / b

1

Division

a %% b

1

Modulus

a + b

2

Addition

a - b

2

Subtraction

a >> b

3

Shift operand a right by b bits. Noninteger operands for this operator are first cast to integers before the bits are shifted.

a << b

3

Shift operand a left by b bits. Noninteger operands for this operator are first cast to integers before the bits are shifted.

a > b

4

Comparison of the first operand greater than the second operand

a < b

4

Comparison of the first operand less than the second operand

a >= b

4

Comparison of the first operand greater than or equal to the second operand

a <= b

4

Comparison of the first operand less than or equal to the second operand

a == b

5

Comparison of equality of two operands

a ~= b

5

Comparison of inequality of two operands

a != b

5

Comparison of inequality of two operands

a <> b

5

Comparison of inequality of two operands

a & b

6

One of the following:

  • Bitwise AND of two operands

    Enabled when Enable C-bit operations is selected in the Chart properties dialog. See Specifying Chart Properties.

  • Logical AND of two operands

    Enabled when Enable C-bit operations is cleared in the Chart properties dialog.

a ^ b

7

One of the following:

  • Bitwise XOR of two operands

Enabled when Enable C-bit operations is selected in the Chart properties dialog. See Specifying Chart Properties.

  • Operand a raised to power b

Enabled when Enable C-bit operations is cleared in the Chart properties dialog.

a | b

8

One of the following:

  • Bitwise OR of two operands

Enabled when Enable C-bit operations is selected in the Chart properties dialog. See Specifying Chart Properties.

  • Logical OR of two operands

Enabled when Enable C-bit operations is cleared in the Chart properties dialog.

a && b

9

Logical AND of two operands

a || b

10

Logical OR of two operands

Unary Operations

The following unary operators are supported in Stateflow action language. Unary operators have higher precedence than binary operators and are evaluated right to left (right associative).

Example

Description

~a

Logical NOT of a

Complement of a (if bitops is enabled)

!a

Logical NOT of a

-a

Negative of a

Unary Actions

The following unary actions are supported in Stateflow action language.

Example

Description

a++

Increment a

a--

Decrement a

Assignment Operations

The following assignment operations are supported in Stateflow action language.

Example

Description

a = expression

Simple assignment

a := expression

Used primarily with fixed-point numbers. See Assignment (=, :=) Operations for a detailed description.

a += expression

Equivalent to a = a + expression

a -= expression

Equivalent to a = a - expression

a *= expression

Equivalent to a = a * expression

a /= expression

Equivalent to a = a / expression

The following assignment operations are supported in Stateflow action language when Enable C-bit operations is selected in the properties dialog for the chart. See Specifying Chart Properties.

Example

Description

a |= expression

Equivalent to a = a | expression (bit operation). See operation a | b in Binary and Bitwise Operations.

a &= expression

Equivalent to a = a & expression (bit operation). See operation a & b in Binary and Bitwise Operations.

a ^= expression

Equivalent to a = a ^ expression (bit operation). See operation a ^ b in Binary and Bitwise Operations.

Pointer and Address Operations

The address operator is available for use with both custom code variables and Stateflow variables. The pointer operator is available for use with custom code variables only.

The following examples show syntax that is valid for use with custom code variables only.

varStruct.field = <expression>;
(*varPtr) = <expression>;
varPtr->field = <expression>;
myVar = varPtr->field;
varPtrArray[index]->field = <expression>;
varPtrArray[expression]->field = <expression>;
myVar = varPtrArray[expression]->field;

The following examples show syntax that is valid for use both with custom code variables and with Stateflow variables.

varPtr = &var;
ptr = &varArray[<expression>];
*(&var) = <expression>;
function(&varA, &varB, &varC);
function(&sf.varArray[<expression>]);

Type Cast Operations

You can use type cast operators to convert a value of one type to a value that can be represented in another type. Normally, you do not need to use type cast operators in actions because Stateflow software checks whether the types involved in a variable assignment differ and compensates by inserting the required type cast operator of the target language (typically C) in the generated code. However, external (custom) code might require data in a different type from those currently available. In this case, Stateflow software cannot determine the required type casts, and you must explicitly use a type cast operator to specify the target language type cast operator to generate.

For example, you might have a custom code function that requires integer RGB values for a graphic plot. You might have these values in Stateflow data, but only in data of type double. To call this function, you must type cast the original data and assign the result to integers, which you use as arguments to the function.

Stateflow type cast operations have two forms: the MATLAB® type cast form and the explicit form using the cast operator. These operators and the special type operator, which works with the explicit cast operator, are described in the topics that follow.

MATLAB® Form Type Cast Operators

The MATLAB type casting form has the general form

<type_op>(<expression>)

<type_op> is a conversion type operator that can be double, single, int32, int16, int8, uint32, uint16, uint8, or boolean. <expression> is the expression to be converted. For example, you can cast the expression x+3 to a 16-bit unsigned integer and assign its value to the data y as follows:

y = uint16(x+3)

Explicit Type Cast Operator

You can also type cast with the explicit cast operator, which has the following general form:

cast(<expression>,<type>)

As in the preceding example, the statement

y = cast(x+3,uint16)

will cast the expression x+3 to a 16-bit unsigned integer and assign it to y, which can be of any type.

type Operator

To make type casting more convenient, you can use a type operator that works with the explicit type cast operator cast to let you assign types to data based on the types of other data.

The type operator returns the type of an existing Stateflow data according to the general form

type(<data>)

where <data> is the data whose type you want to return.

The return value from a type operation can be used only in an explicit cast operation. For example, if you want to convert the data y to the same type as that of data z, use the following statement:

cast(y,type(z))

In this case, the data z can have any acceptable Stateflow type.

Replacing Operators with Target Functions

The target function library published by Real-Time Workshop® Embedded Coder™ code generation software allows you to replace a subset of arithmetic operators with target functions. Operator entries of the target function library can specify integral or fixed-point operand and result patterns. Operator entries may be used for the following built-in operators:

+
-
*
/

For example, you can replace an expression such as y = u1 + u2 with a target function, provided that u1, u2, and y have types that permit a match with an addition entry in the target function library.

Stateflow chart semantics may limit operator entry matching because it uses the target integer size as its intermediate type in all arithmetic expressions. For example, suppose a Stateflow action contains this arithmetic expression:

y = (u1 + u2) % 3

This expression computes the intermediate addition into a target integer. If the target integer size is 32 bits, you cannot replace this expression with a target function library operator entry for addition that produces a signed 16-bit result without loss of precision.

To learn how to create and register function replacement tables in a target function library, see Target Function Libraries in theReal-Time Workshop Embedded Coder User's Guide. To select and view target function libraries, see Selecting and Viewing Target Function Libraries in the Real-Time Workshop® User's Guide.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS