convert binary to decimal from column vector of binary bits?

3 views (last 30 days)
I have a column vector in binary consisting of thousands of 32-bit words. Each word has three value, made up of 1, 10 and 21 bits. I need to convert each of those values into decimals for all the thousands of word. the first bit just tells me what the rest of the word means. the 10 bit value is a number from 1 to 1024 and the next 21 bits is a number from 1 to 2097151. This is the code I have so far (rawbidata is the column vector of rawbinary bits). I keep getting "Input must be a string." Any thoughts?
rawend=length(rawbidata);
strrawbidata=num2str(rawbidata,'%-1d')
for i=1:32:rawend
if abs((strrawbidata(x))-1)>0.5
procdata(z,1)=bin2dec(transpose(strrawbi
procdata(z,2)=bin2dec(transpose(strrawbi
z=z+1;
x=x+32
end

Answers (2)

Walter Roberson
Walter Roberson on 4 Apr 2012
strrawbidata contains the character '0' and the character '1'. Numerically those values are 48 and 49. Subtract one and take the absolute value and you get 47 and 48. Those are always greater than 0.5.
Your actual code seems to be clipped so we cannot see exactly what you are sending to bin2dec()
Note: instead of using num2str() on rawbidata, you can use
strrawbidata = char(rawbidata + '0') .';
which includes the transpose, allowing you to simplify your code.

CWill
CWill on 4 Apr 2012
The spacing got messed up:
rawend=length(rawbidata);
strrawbidata=num2str(rawbidata,'%-1d');
for i=1:32:rawend if abs((strrawbidata(x))-1)>0.5 procdata(z,1)=bin2dec(transpose(strrawbidata(x+1:x+11))); procdata(z,2)=bin2dec(transpose(strrawbidata(x+12:x+32)*0.000001))+(t*2.097152); z=z+1; end
x=x+32 end

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!