problem with typecast command

36 views (last 30 days)
Jatin Arora
Jatin Arora on 16 May 2013
I have a array of dimensions 199*8180 of uint 8 type. I am using typecast command to convert it into unit16 type but I am getting an error message. The message is
Error using typecast The first input argument must be a vector.
can someone please help me with this.
Regards, Jatin

Answers (2)

Image Analyst
Image Analyst on 16 May 2013
Try cast() instead of typecast().
image16 = cast(image8, 'uint16');
or simply
image16 = uint16(image8);
Multiply image8 by 256 before casting if you want to brighten it.
  1 Comment
Shashank Prasanna
Shashank Prasanna on 16 May 2013
IA, cast simple changes the datatype represented in ML where as typecast changes the underlying representation without changing the data. This is useful say when a hardware returns uint16 through a serial connection (but the data is actually double), you would need to typecast it. cast function here would simply change class uint16 to class double keeping the same data. The ambiguity arises from the nomenclature.
Here is an excerpt 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.

Sign in to comment.


Shashank Prasanna
Shashank Prasanna on 16 May 2013
typecast only takes scalars or vectors. If you have a matrix you can convert it into a vector from a matrix and then use typecast
>> datauint8 = data(:);
>> datauint16 = typecast(datauint8,'uint16');
>> datamat = reshape(datauint16,199,8180);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!