cast - Convert variable to different data type - MATLAB (original) (raw)
Convert variable to different data type
Syntax
Description
[B](#mw%5Fe6e82388-a147-4fd8-a455-0a64181b5fc9) = cast([A](#mw%5Fbafb4d74-9bd5-4b33-b129-8f4000f6efbd),[newclass](#mw%5F95e4dd69-4cf8-41a9-a746-08560de273e1))
returns the data in A
converted to the data type (class)newclass
, where newclass
is the name of a built-in data type compatible with A
. Any values in A
that are outside the range of newclass
are truncated in B
to the nearest endpoint.
When converting a floating-point number to an integer, the cast
function rounds the number to the nearest integer. If the floating-point number has a fractional part of exactly 0.5, then it rounds away from zero to the integer with larger magnitude.
[B](#mw%5Fe6e82388-a147-4fd8-a455-0a64181b5fc9) = cast([A](#mw%5Fbafb4d74-9bd5-4b33-b129-8f4000f6efbd),like=[p](#mw%5F9aefb75b-feb7-46a1-a99f-ff7851520618))
converts A
to the same data type and sparsity as the variablep
. The complexity (real or complex) of B
is determined by both A
and p
. If bothA
and p
are real, then B
is also real. Otherwise, B
is complex.
Examples
Convert int8
values to uint8
.
Define a vector of 8-bit integers.
Convert a
to unsigned 8-bit integers. The –5 value outside the range of uint8
is truncated to 0.
b = 1×2 uint8 row vector
0 5
Create a 1-by-3 vector of 32-bit signed integers.
A = 1×3 int32 row vector
-12 34 56
Create a complex number of the data type double
.
Convert A
to the same data type and complexity as the variable p
.
B = 1×3 complex
-12.0000 + 0.0000i 34.0000 + 0.0000i 56.0000 + 0.0000i
Create a 2-by-3 matrix of zeros whose elements are 32-bit unsigned integers.
A = 2×3 uint32 matrix
0 0 0 0 0 0
Create a 2-by-2 sparse matrix of the data type double
.
p = 2×2 sparse double matrix (1 nonzero) (2,2) 3.1416
Convert A
to the same data type and sparsity as the variable p
.
B = 2×3 sparse double matrix All zero
Input Arguments
Input array, specified as a scalar, vector, matrix, or multidimensional array. The data type of A
can be a built-in data type or other data type that supports conversion to the specified new data type.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
Complex Number Support: Yes
New class, specified as "single"
, "double"
,"int8"
, "int16"
, "int32"
,"int64"
, "uint8"
, "uint16"
,"uint32"
, "uint64"
,"logical"
, or "char"
.
Prototype, specified as a scalar, vector, matrix, or multidimensional array. The data type of p
can be a built-in data type or other compatible data type that supports conversion from the input data type.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
Complex Number Support: Yes
Output Arguments
Output array, returned as a scalar, vector, matrix, or multidimensional array.
Extended Capabilities
Usage notes and limitations:
- Enumeration inputs must be scalar valued at compile time. Arrays of enumerations are not supported.
Usage notes and limitations:
- Enumeration inputs must be scalar valued at compile time. Arrays of enumerations are not supported.
The cast
function supports GPU array input with these usage notes and limitations:
- For the syntax
B = cast(A,like=p)
, the outputB
is agpuArray
if and only ifp
is agpuArray
.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Usage notes and limitations:
- For the syntax
B = cast(A,like=p)
, the outputB
is a distributed array if and only ifp
is a distributed array.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Starting in R2022a, the syntax cast(A,like=p)
returns output consistent with the prototype p
when the data type ofp
is a subclass of MATLAB® numeric types.
For example, this code now returns an output that has the same data type asp
:
p = matlab.lang.OnOffSwitchState.on; x = cast(1,like=p)
x =
OnOffSwitchState enumeration
on
In previous releases, the code returns x = 1
with data type logical
.
Starting in R2021b, the newclass
input argument of the syntaxcast(A,newclass)
is case-sensitive. You must specifynewclass
as a character vector or a string of lowercase letters that represents the new data type.
For example, to convert a double
value to the int8
data type, you must use cast(1.234,"int8")
. The commandcast(1.234,"Int8")
now errors.