Documentation Center |
On this page… |
---|
Because MATLAB^{®} software does not have type declarations, an assignment like A = B replaces the type and content of A with the type and content of B. If A does not exist at the time of the assignment, MATLAB creates the variable A and assigns it the same type and value as B. Such assignment happens with all types in MATLAB—objects and built-in types alike—including fi, double, single, int8, uint8, int16, etc.
For example, the following code overwrites the value and int8 type of A with the value and int16 type of B:
A = int8(0); B = int16(32767); A = B A = 32767 class(A) ans = int16
You may find it useful to cast data into another type—for example, when you are casting data from an accumulator to memory. There are several ways to cast data in MATLAB. The following sections provide examples of three different methods:
Casting by Subscripted Assignment
Casting by Conversion Function
Casting with the Fixed-Point Designer™ reinterpretcast Function
Casting with the cast Function
The following subscripted assignment statement retains the type of A and saturates the value of B to an int8:
A = int8(0); B = int16(32767); A(:) = B A = 127 class(A) ans = int8
The same is true for fi objects:
fipref('NumericTypeDisplay', 'short'); A = fi(0, true, 8, 0); B = fi(32767, true, 16, 0); A(:) = B A = 127 s8,0
Note For more information on subscripted assignments, see the subsasgn function. |
You can convert from one data type to another by using a conversion function. In this example, A does not have to be predefined because it is overwritten.
B = int16(32767); A = int8(B) A = 127 class(A) ans = int8
The same is true for fi objects:
B = fi(32767, true, 16, 0) A = fi(B, 1, 8, 0) B = 32767 s16,0 A = 127 s8,0
Using a numerictype Object in the fi Conversion Function. Often a specific numerictype is used in many places, and it is convenient to predefine numerictype objects for use in the conversion functions. Predefining these objects is a good practice because it also puts the data type specification in one place.
T8 = numerictype(1,8,0) T8 = DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 0 T16 = numerictype(1,16,0) T16 = DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 0 B = fi(32767,T16) B = 32767 s16,0 A = fi(B, T8) A = 127 s8,0
You can convert fixed-point and built-in data types without changing the underlying data. The Fixed-Point Designer reinterpretcast function performs this type of conversion.
In the following example, B is an unsigned fi object with a word length of 8 bits and a fraction length of 5 bits. The reinterpretcast function converts B into a signed fi object A with a word length of 8 bits and a fraction length of 1 bit. The real-world values of A and B differ, but their binary representations are the same.
B = fi([pi/4 1 pi/2 4], false, 8, 5) T = numerictype(true, 8, 1); A = reinterpretcast(B, T) B = 0.7813 1.0000 1.5625 4.0000 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 5 A = 12.5000 16.0000 25.0000 -64.0000 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 1
To verify that the underlying data has not changed, compare the binary representations of A and B:
binary_B = bin(B) binary_A = bin(A) binary_A = 00011001 00100000 00110010 10000000 binary_B = 00011001 00100000 00110010 10000000
Using the cast function, you can convert the value of a variable to the same numerictype, complexity, and fimath as another variable.
In the following example, a is cast to the data type of b. The output, c, has the same numerictype and fimath properties as b, and the value of a.
a = pi; b = fi([],1,16,13,'RoundingMethod',Floor); c= cast(a,'like',b) c = 3.1415 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13 RoundingMethod: Floor OverflowAction: Saturate ProductMode: FullPrecision SumMode: FullPrecision
Using this syntax allows you to specify data types separately from your algorithmic code as described in Manual Fixed-Point Conversion Best Practices.