Search code examples
arraysmatlabbinaryhexcell-array

Using a 'for loop' to iterate through a cell array


In matlab, I have a cell array block (s) with hex values.

a = '40 C0 70 EB'; 
b = '40 C0 80 94'; 
c = '40 C0 90 59';
s = {a;b;c};

I want to iterate horizontally through each line in such a way that;

  1. first byte 'EB' must be converted to binary ( i.e. EB = 1110 1011 = 8 bits) and saved in some variable/array

  2. Then, 'EB & 70' must be converted to binary but their binary values must be stored together (i.e. EB & 70 = 11101011 01110000 = 16 bits) in some variable/array.

  3. Similarly, 'EB & 70 & C0' converted to binary (i.e. EB & 70 & C0 = 11101011 01110000 11000000 = 24 bits) in some variable/array.

  4. Similarly, '40 C0 70 EB' (i.e. 40 & C0 & 70 & EB = 11101011 01110000 11000000 01000000 = 32 bits)

  5. Finally, same thing has to be carried out for the rest of the lines.

I have written a code to convert individual hex values into their equivalent binary but I am not sure how to proceed from here on.

a = '40 C0 70 EB'; 
b = '40 C0 80 94'; 
c = '40 C0 90 59'; 

s = {a;b;c};

s = cellfun(@strsplit, s, 'UniformOutput', false);

s = vertcat(s{:}); 

dec = hex2dec(s);
bin = dec2bin(dec);
x=cellstr(bin);
bin = mat2cell(x, repmat(size(s,1),1,size(s,2)));    

Any suggestions on how to accomplish these feats?


Solution

  • From the code you've included in your question it seems you're most of the way there.

    This bit I think you're missing is how to concatenate binary words, which is a bit awkward in Matlab. See this post for some tips. However for your example the slightly hack-y option of just converting to strings and concatenating might be easier.

    Making use of your code, the example below outputs:

    '11101011'    '1110101101110000'    '111010110111000011000000'    '11101011011100001100000001000000'
    '10010100'    '1001010010000000'    '100101001000000011000000'    '10010100100000001100000001000000'
    '01011001'    '0101100110010000'    '010110011001000011000000'    '01011001100100001100000001000000'
    

    which I think is what you want, but wasn't totally sure from your text. I assume you want to keep all 4 numbers (8bit, 16bit, 24bit and 32bit) from each row, so have a total of 12 binary strings.

    a = '40 C0 70 EB'; 
    b = '40 C0 80 94'; 
    c = '40 C0 90 59'; 
    
    s = {a;b;c};
    s = cellfun(@strsplit, s, 'UniformOutput', false);
    s = vertcat(s{:});
    
    % Empty cell to store output binary strings;
    outputBinary = cell(size(s));
    outputDec = zeros(size(s));
    
    % Iterate over each row
    for rowNum = 1:size(s,1)
    
        % To build up binary string from left to right
        binaryString = [];
    
        % Iterate over each column
        for colNum = 1:size(s,2)
    
            % Convert hex -> dec -> 8-bit binary word
            % and add new word to end of growing string for this row
            thisBinary = dec2bin(hex2dec(s{rowNum,end+1-colNum}), 8);
            binaryString = [binaryString, thisBinary]; %#ok<AGROW>
    
            % Save solution to output array:
            outputBinary{rowNum, colNum} = binaryString;
            outputDec(rowNum, colNum) = bin2dec(binaryString);
        end
    end
    
    % Display result
    format long;
    disp(outputBinary);