DonMano
DonMano

Reputation: 3

Concatenate binary data in matlab?

I would like to create a 10 bit binary value by concatenating two 4-bit value and one 2-bit value. eg: {2'b11,4'b1010,4'b1100}.

How would I achieve this?

Upvotes: 0

Views: 5268

Answers (2)

Hoki
Hoki

Reputation: 11792

Quite a few options there depending on how you want your final result:

bin2b  = '11' ;
bin4b1 = '1010' ;
bin4b2 = '1100' ;

b10str = strcat(bin2b,bin4b1,bin4b2)    %// 10 bit value as a string type
b10str = [bin2b bin4b1 bin4b2]          %// 10 bit value as a string type (same than above, shorthand notation for concatenation)

b10dec = bin2dec(b10str)                %// 10 bit value as a numeric type (decimal base)

b10hex = dec2hex(bin2dec(b10str))       %// string type again (hexadecimal base)

b10bitarray = de2bi(b10dec)             %// array of 10 boolean (each represent one bit)

That will give you:

b10str =
1110101100
b10dec =
   940
b10hex =
3AC
b10bitarray =
     0     0     1     1     0     1     0     1     1     1

Note: On most PC, binary ordering is "lower endian". Depending on the endianness, you may want to "flip" your bit array before converting it, which can be done with fliplr:

>> fliplr(b10str)
ans =
0011010111

and then convert as above

Upvotes: 1

meneldal
meneldal

Reputation: 1737

As far as I know the best work around is to write a function that does this in C/C++/whatever and use the MEX API to call it from Matlab.

Matlab doesn't let you process types that are not really supported in that way or if you really want to do it in Matlab it will be very ugly and slow.

Upvotes: 0

Related Questions