MATLAB: Returning Byte array of given substring

communicationdigital signal processingmatlab functionsignalsignal processing

Hi guys , I've implemented in matlab a function that gets as its input an array of binary values (0 or 1) and it returns the Byte value of every 8 bit in my input array.
this means if I input to it [1 1 1 1 1 1 1 1] , the returned value is [255 0 0 0 0 0 0 0 …%34 zeros%], another example if I input to it 16 bit array of 1
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] , so my function returns an array [255 255 0 0 0 0 0 0 0 …%32 zeros..%] -I just wrote 32 zeros in my array which means 0 0 0 0 .. 32 times. it should return twice 255 because first 8bit from left to right of my array is 255 in unsigned int, and the other following 8bit of 1 also 255 in unsigned int .. so my function return twice 255 , first 255 for first 8bit of 1, and the second 255 value at index 2 is the second 8bit of 1 in my input array.
I have implemented this in matlab but it doesn't give me correct answers, any help please? the input is always its length divisible by 8.(the input length is multiplication of 8)
this what Im implementing called ByteConvertRxBuffer and return an array of Byte values(unsgined integers array values)
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
ByteConvertRxBuffer = zeros(1,35,'uint8'); %starting array with size 35 which all its %values are zero
ireal = 0;
% int byteSize=8;
%warning: some of the below code becomes invalid if byteSize exceeds 32767
byteSize = 8 ;
i = 0; %#ok<NASGU> %size_t i inside block
for i = 0: byteSize: (numel(rx))
nibble = zeros(1,byteSize); %for taking every 8bit of my input seperately
for j = 1 : 1 : byteSize %taking every 8bit of my input seperately
nibble(j) = rx(j+i);
end %after it finishes 8 iterations then out from the current loop
clear j %int j was local to block
ByteConvertRxBuffer(ireal) = EvaluateBinary(nibble); % here I want to put every value of this returned function to ByteConverRxBuffer array
ireal=ireal+1;
end
Im calling this function:
function total=EvaluateBinary(substring)
byteSize=8;
total = uint8(0); %this varibal is unsigned integer of 8bit
counter = uint8(1);
for i=byteSize:-1:1
if (substring(i) == 1)
total=total + counter; end
counter = counter*2;
end

Best Answer

function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
rx=num2str(rx);
rx=rx(rx~=' ');
rx=bin2dec(reshape(rx,8,[])')';
ByteConvertRxBuffer=[rx,zeros(1,35-length(rx))];
end
Related Question