2
votes

I have 5 strings (hex) which look like this:

a = 40 C0 70 EB;
b = 40 C0 80 94;
c = 40 C0 90 59;
d = 40 C0 A0 13;
e = 40 C0 B0 DE

I need to extract each column (vertical values) starting from the last then save it into a string and then convert it into its equivalent binary. I hope the figure below gives a clear picture.

enter image description here

I was able to convert the given hex to binary values horizontally one at a time, but now I would like to extract them vertically and then convert them to binary. The code which I have written is as follows,

Data = '40 C0 70 EB';         
str = regexp(Data,' ','split');
Ind = cellfun(@length,str);
str=str(Ind==2);
%Hex to Binary 
bin = hexToBinaryVector(str,8,'MSBFirst');

Desired output: When the values in the first column(input) are retrieved they should be converted into binary and stored as an array(output). For instance, for 1st column (red), we have values 'EB 94 59 13 DE' its equivalent binary is ['1001 0100' '1001 0101' '1001 0110' '1001 0111' '1001 1000' '1001 1001']. Therefore, all these binary values must be stored as an array under one variable. Similarly, others should also be stored as an array under different variables.

2
The question appears to be well explained, but I can't seem to understand what you want,,, Can you provide the input and desired output in plain text?Stewie Griffin
Doesn't regexp(Data,' ','split'); do the same as strsplit(Data)?Adriaan

2 Answers

0
votes

Here's a way to do it:

s = {a; b; c; d; e}; % Collect into cell array
s = cellfun(@strsplit, s, 'UniformOutput', false); % split each string by space
s = vertcat(s{:}); % arrange as 2D-cell array of strings
s = fliplr(s); % reverse horizontally. Last column becomes first
y = dec2bin(hex2dec(s), 8); % convert each string (in linear order) to binary
y = mat2cell(y, repmat(size(s,1),1,size(s,2))); % split results into a cell array

The result is a cell array of 2D char arrays of zeros and ones. In your example this gives

y{1} =
11101011
10010100
01011001
00010011
11011110
...
y{4} =
01000000
01000000
01000000
01000000
01000000
0
votes

In the famous words of Mark Twain:

'I didn't have time to write a short letter code, so I wrote a long one instead.'

You can combine these functions into fewer functions, but this way it's easy to see what each of the lines does. You don't need both g and h for instance. Also, chose more descriptive names than those I have used in my code!

I don't have the Data Acquisition Toolbox so I have to do dec2bin(hex2dec(hexa)). Luckily for you, you can skip a function! =)

a = '40 C0 70 EB'; b = '40 C0 80 94'; c = '40 C0 90 59'; d = '40 C0 A0 13'; e = '40 C0 B0 DE';
cell_block = {a;b;c;d;e}
cell_block = 
    '40 C0 70 EB'
    '40 C0 80 94'
    '40 C0 90 59'
    '40 C0 A0 13'
    '40 C0 B0 DE'

f = @(x) cellfun(@strsplit, x, 'UniformOutput', false);
g = @(x) cellfun(@hex2dec, x, 'UniformOutput', false).';
h = @(x) cellfun(@dec2bin, x, 'UniformOutput', false);
cells_of_binary_values = h(g(f(cell_block)));
arrayfun(@(ii) reshape(cells_of_binary_values{ii}.', 1, []), ...
         1:numel(cells_of_binary_values),'UniformOutput',0)

ans = 
{
  [1,1] = 01000000110000000111000011101011
  [1,2] = 01000000110000001000000010010100
  [1,3] = 01000000110000001001000001011001
  [1,4] = 01000000110000001010000000010011
  [1,5] = 01000000110000001011000011011110