Section: Type Conversion Functions
y = int16(x)
where x
is an n
-dimensional numerical array. Conversion
follows the general C rules (e.g., if x
is outside the normal
range for a signed 16-bit integer of [-32768,32767]
, the least significant
16 bits of x
are used after conversion to a signed integer). Note that
both NaN
and Inf
both map to 0.
int16
. First, the routine uses
--> int16(100) ans = 100 --> int16(-100) ans = -100
In the next example, an integer outside the range of the type is passed in. The result is the 16 least significant bits of the argument.
--> int16(40000) ans = -25536
In the next example, a positive double precision argument is passed in. The result is the signed integer that is closest to the argument.
--> int16(pi) ans = 3
In the next example, a complex argument is passed in. The result is the signed integer that is closest to the real part of the argument.
--> int16(5+2*i) ans = 5
In the next example, a string argument is passed in. The string argument is converted into an integer array corresponding to the ASCII values of each character.
--> int16('helo') ans = 104 101 108 111
In the last example, a cell-array is passed in. For cell-arrays and structure arrays, the result is an error.
--> int16({4}) Error: Cannot convert cell-arrays to any other type.