askvity

How to convert double to uint16 in MATLAB?

Published in MATLAB Data Types 4 mins read

In MATLAB, you can convert a double array or scalar to a uint16 array or scalar by using the uint16 function.

Converting Double to uint16 using the uint16 Function

MATLAB provides built-in type conversion functions that allow you to change the data type of variables. To convert a variable from double to uint16, you use the function with the target type's name, which is uint16.

As mentioned in the reference, if you have an array of a different type, such as double or single, you can convert that array to an array of type uint16 by using the uint16 function. This function takes your existing variable as an input and returns a new variable of the uint16 data type.

Here's the basic syntax:

uint16_variable = uint16(double_variable);

Example

Let's say you have a double variable named myDoubleValue and you want to convert it to uint16.

% Define a double variable
myDoubleValue = 65535.5;
anotherDouble = 100.7;
aNegativeDouble = -5.2;
aLargeDouble = 70000.1;

% Convert the double values to uint16
myUint16Value = uint16(myDoubleValue);
anotherUint16 = uint16(anotherDouble);
negativeConverted = uint16(aNegativeDouble);
largeConverted = uint16(aLargeDouble);

% Display the results and their data types
disp(['Original double value 1: ', num2str(myDoubleValue), ' (Type: ', class(myDoubleValue), ')']);
disp(['Converted uint16 value 1: ', num2str(myUint16Value), ' (Type: ', class(myUint16Value), ')']);

disp(['Original double value 2: ', num2str(anotherDouble), ' (Type: ', class(anotherDouble), ')']);
disp(['Converted uint16 value 2: ', num2str(anotherUint16), ' (Type: ', class(anotherUint16), ')']);

disp(['Original double value 3: ', num2str(aNegativeDouble), ' (Type: ', class(aNegativeDouble), ')']);
disp(['Converted uint16 value 3: ', num2str(negativeConverted), ' (Type: ', class(negativeConverted), ')']);

disp(['Original double value 4: ', num2str(aLargeDouble), ' (Type: ', class(aLargeDouble), ')']);
disp(['Converted uint16 value 4: ', num2str(largeConverted), ' (Type: ', class(largeConverted), ')']);

When you run this code, you will observe the following:

  • myDoubleValue (65535.5) will be converted to uint16(65535.5), which becomes 65536.
  • anotherDouble (100.7) will be converted to uint16(100.7), which becomes 101.
  • aNegativeDouble (-5.2) will be converted to uint16(-5.2), which becomes 0.
  • aLargeDouble (70000.1) will be converted to uint16(70000.1), which becomes 65535.

Practical Considerations

When converting from double to uint16, keep the following in mind:

  • Rounding: MATLAB rounds the floating-point double values to the nearest integer before converting them to uint16. For values exactly halfway between two integers, it rounds away from zero.
  • Clipping: The uint16 data type can only store non-negative integer values ranging from 0 to 65535.
    • Any double value less than 0 will be converted to 0.
    • Any double value greater than 65535 will be converted to 65535.

This behavior is known as clipping or saturation, where values outside the target data type's range are limited to the minimum or maximum value of that range.

Conversion Behavior Summary

Input double Value Output uint16 Value Notes
100.7 101 Rounded
65535.5 65536 Rounded (away from zero)
-5.2 0 Clipped to minimum
70000.1 65535 Clipped to maximum
30000 30000 Exact integer within range

Utilizing the uint16 function is the standard and straightforward way to perform this data type conversion in MATLAB, ensuring that your data conforms to the specific requirements of the unsigned 16-bit integer format.

Related Articles