# Read binary data as three UBYTE (8-bit Unsigned Byte) in MATLAB and use bit shift operators to get two 12 bit streams

11 views (last 30 days)
Eliza on 15 Dec 2020
Commented: Eliza on 22 Dec 2020
I need to read the data as UBYTE (8-bit Unsigned Byte) in MATLAB and then used bit shift operators to get two 12-bit streams with 3600000 samples. I have to do that by a command that: the first two 12 bit values are contained in the first 3 UBYTEs, 8 bits from the first byte with the first 4 bits from the second byte, then the first 4 bits from byte 2 with all 8 bits from byte 3. Then the process repeats with the next 3 bytes (byte 4-6 etc.) and so on. The related commands are as follows.
How can I apply this command in MATLAB?
Walter Roberson on 21 Dec 2020
You do not need to read three bytes and do shift arithmetic. I show how you can read ubit4=>ubit16 and then put together the nibbles.

Steven Lord on 15 Dec 2020
Use the bit-wise operations. You're also going to need to convert between data types.
x = int8(5)
y = int16(x)
You may need or want to read the "Largest and Smallest Values for Integer Classes" section on this documentation page before you start writing your code.
##### 2 CommentsShowHide 1 older comment
Eliza on 20 Dec 2020
Thank you for your guidance.

### More Answers (1)

Walter Roberson on 16 Dec 2020
I believe that you should be able to implement the needed transformation by using fread() with 'ubit12=>uint16' and then using swapbytes() https://www.mathworks.com/help/matlab/ref/swapbytes.html
Eliza on 22 Dec 2020
Thank you so much for your perfect answer.