Byte Array / Bit Conversions in a Cell

42 views (last 30 days)
Eore D
Eore D on 22 Jun 2021
Commented: Eore D on 22 Jun 2021
I'm not too familiar with Matlab, and I'm having a trouble with cell operations. I have 2 questions:
1- ) Think a byte array cell which consist of 1000x4 uint8 type. I want to convert it into a cell which is 1000x1 uint32 type. Byte order of byte array is big endian. I'm having a trouble with implementing the 'typecast' method to this cell without any loop for time efficiency.
2-) Again think the uint8 type cell which is 1000x1. I need to split it to bits. As a result I want to obtain 8 cells 1000x1 for every bit. But again I could not handle with the 'bitget' method for cells.
Thanks a lot!

Accepted Answer

Steven Lord
Steven Lord on 22 Jun 2021
If all your numeric data is the same type, I recommend storing it in a numeric array rather than as a cell array. Let's make some sample int8 data:
rng default
x = randi([0 intmax('int8')], 3, 4, 'int8')
x = 3×4
104 116 35 123 115 80 70 20 16 12 122 124
Now to convert this to int32 we need to first make it a vector. I'm going to convert each row of x into an int32 so:
xvec = reshape(x.', 1, [])
xvec = 1×12
104 116 35 123 115 80 70 20 16 12 122 124
y = typecast(xvec, 'int32').'
y = 3×1
2065921128 340152435 2088373264
Let's look at the hex digit patterns.
format hex
disp(x)
68 74 23 7b 73 50 46 14 10 0c 7a 7c
disp(y)
7b237468 14465073 7c7a0c10
Or perhaps you want the first column of x to have the most significant bits in y, the second column the next most significant, etc.? If so take a look at the swapbytes function.
disp(swapbytes(y))
6874237b 73504614 100c7a7c

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!