Convert data types without changing underlying data
Y = typecast(X, type)
Y = typecast(X, type) converts
a numeric value in
X to the data type specified
X must be a full,
noncomplex, numeric scalar or vector. The
is a string set to one of the following:
typecast is different from the MATLAB®
cast function in that it does not alter
the input data.
typecast always returns the same
number of bytes in the output
Y as were in the
X. For example, casting the 16-bit integer
the full 16 bits in two 8-bit segments (3 and 232) thus keeping its
original value (3*256 + 232 = 1000). The
on the other hand, truncates the input value to 255.
The output of
typecast can be formatted
differently depending on what system you use it on. Some computer
systems store data starting with its most significant byte (an ordering
called big-endian), while others start with the
least significant byte (called little-endian).
MATLAB issues an error if
This example converts between data types of the same size:
typecast(uint8(255), 'int8') ans = -1 typecast(int16(-1), 'uint16') ans = 65535
Set X to a 1-by-3 vector of 32-bit integers, then cast it to an 8-bit integer type:
X = uint32([1 255 256]) X = 1 255 256
Running this on a little-endian system produces the following results. Each 32-bit value is divided up into four 8-bit segments:
Y = typecast(X, 'uint8') Y = 1 0 0 0 255 0 0 0 0 1 0 0
The third element of
X, 256, exceeds the
8 bits that it is being converted to in
thus overflows to
Y(9:12) ans = 0 1 0 0
length(Y) is equal to
Also note the difference between the output of
Z = cast(X, 'uint8') Z = 1 255 255
This example casts a smaller data type (
into a larger one (
uint16). Displaying the numbers
in hexadecimal format makes it easier to see just how the data is
format hex X = uint8([44 55 66 77]) X = 2c 37 42 4d
typecast is done on a big-endian
system. The four 8-bit segments of the input data are combined to
produce two 16-bit segments:
Y = typecast(X, 'uint16') Y = 2c37 424d
The second is done on a little-endian system. Note the difference in byte ordering:
Y = typecast(X, 'uint16') Y = 372c 4d42
You can format the little-endian output into big-endian (and
vice versa) using the
Y = swapbytes(typecast(X, 'uint16')) Y = 2c37 424d
This example attempts to make a 32-bit value from a vector of three 8-bit values. MATLAB issues an error because there are an insufficient number of bytes in the input:
format hex typecast(uint8([120 86 52]), 'uint32') Error using typecast Too few input values to make output type.
Repeat the example, but with a vector of four 8-bit values, and it returns the expected answer:
typecast(uint8([120 86 52 18]), 'uint32') ans = 12345678
Usage notes and limitations:
Value of data type argument must be lowercase.
When you use
typecast with inheritance
of input port data types in MATLAB Function blocks,
you can receive a size error. To avoid this error, specify the block
input port data types explicitly.
Integer input or result classes must map directly to a C type on the target hardware.
The input must be a variable-length vector or a fixed-size vector. See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
The output vector always has the same orientation as the input vector.