| Stateflow User's Guide | ![]() |
Stateflow provides 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 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 Stateflow. In this case, Stateflow cannot determine the required type casts and you must explicitly use a Stateflow action language type cast operator to tell Stateflow 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.
In 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 in Stateflow has the general form
<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:
Explicit Type Cast Operator
You can also type cast with the explicit cast operator, which has the following general form:
As in the preceding example, the statement
tells Stateflow to 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, Stateflow also provides 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
where <data> is the Stateflow 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:
In this case, the data z can have any acceptable Stateflow type.
| Pointer and Address Operations | Using Special Symbols in Actions | ![]() |
© 1994-2005 The MathWorks, Inc.