How to access a group of bits from a string ?
3 views (last 30 days)
Show older comments
Hello,
I have a problem on how to access a specific range of bits from a given string
Consider I have a 20 bit string,I want to access the bit 1-6 and 7-12 and then 13-20. How do I do it in matlab? please help thanks in advance
Eg: X = 0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0
I want the output as
x1= 0 0 0 0 0 0
x2= 1 1 1 0 0 1
x3= 1 1 1 1 0 0 0 0
I have no idea how to do it.Please help. thanks in advance
0 Comments
Accepted Answer
Image Analyst
on 27 Jan 2015
It's a little ambiguous, but how about just indexing or subtracting '0':
X = '0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0'
% Get rid of any spaces
X(X==' ') = []
% Extract the three substrings
x1 = X(1:6)
x2 = X(7:12)
x3 = X(13:end)
% Convert to a numerical data type.
doubleX = X - '0'
In the command window:
X =
0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0
X =
00000011100111110000
x1 =
000000
x2 =
111001
x3 =
11110000
doubleX =
0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0
I don't see why cell arrays would be necessary unless I misunderstood something.
More Answers (0)
See Also
Categories
Find more on Logical 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!