Uthara Keerthan
Uthara Keerthan

Reputation: 39

What is the difference between cast() and typecast() commands in Matlab

Executing these commands in matlab workspace

a = 10
b = cast(a,'uint8')
c = typecast(a,'uint8')

while I look for the values of b and c I get

b = 10

c = 0 0 0 0 0 0 36 64

Also whos('b') and whos('c') returns uint8

Upvotes: 3

Views: 1921

Answers (1)

DVarga
DVarga

Reputation: 21799

The answer comes from the documentation of typecast:

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 input X. For example, casting the 16-bit integer 1000 to uint8 with typecast returns the full 16 bits in two 8-bit segments (3 and 232) thus keeping its original value (3*256 + 232 = 1000). The cast function, on the other hand, truncates the input value to 255.

Upvotes: 4

Related Questions