Clear Filters
Clear Filters

fread function for reading 10 bit raw file.

44 views (last 30 days)
When I read 10 bit raw file in fread as Uint16 and as uint8 the size of the data is doubled in case of reading as uint8. Wanted to know how fread function internally reads 10 bit data in uint8 and uint16 data type.
sample of my code below:
fid = fopen('2.raw');
data = fread(fid,'uint16');

Accepted Answer

Peter O
Peter O on 10 Mar 2022
Edited: Peter O on 10 Mar 2022
To confirm, you're saying that result of importing the file as UINT8 is double the array size as importing as UINT16? That's to be expected. uint8 will read in blocks of 8 bits from your file (range 0 to 255; e.g. one byte) sequentially, while uint16 will read in blocks of 16 bits (range 0 to 65536, two bytes). If your file is 96 bytes long, then you'll see 96 8-bit values, but only 48 16-bit values.
If you look at their sizes you'll likely see that the array bytes are the same. Each 16-bit value storage uses twice as many bytes.
a = uint8([1,2,3,4]);
b= uint16([1,2]);
Name Size Bytes Class Attributes a 1x4 4 uint8 b 1x2 4 uint16
Important detail I left off: Since you have 10 bit data, assuming it's in block form, neither method is going to import your data correctly, because it does not know that directly. See the documentation for fread. You'll want to specify the bitn version. Use '*ubit10' instead of to place the 10-bit data into uint16 format for MATLAB to use.
data = fread(fid,'*ubit10');

More Answers (1)

Jan on 10 Mar 2022
If the 10bit data are written as bitstream, 8 numbers use 10 bytes. If you read this as UINT8 or UINT16 does not matter: the sequence of bits in the memory is equal. If these bits are interpreted as UINT8 or UINT16 will change the values, of course.
A difference is that the number if bytes need not be even, such that reading the file as UINT16 might skip the last byte.
To import the data use the fomat UBIT10:
fid = fopen('2.raw');
data = fread(fid, 'ubit10=>uint16');




Community Treasure Hunt

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

Start Hunting!