Q:

Group Binary and add a space after 8 numbers Matlab (Duplicate)

I wish to group 8 binary numbers together and then shape it into matrix which is as shown:
01001001  10110100  10111101  10000111
10110101  10100011  10110010  10111000
010

My input will be a MP3 file.  
I manage to add a space after 8 binary, but have no idea how to make it to new line after 4 groups of 8 binary. 
My Matlab programming is:
fid=fopen('mp3file.mp3','r','b');
x=uint8(fread(fid,'ubit1'));
a = 1;

if ( a+100 <= numel(x) )
    B = x(a:a+25);
    str = [repmat('%d', 1, 8) ' '];
    fprintf(str,B);
end

output: 01001001  10110100  10111101  10000111  10110101  10100011 ...

I found there is similar post as this, but that one only apply to characters/alphabet, not binary :(
Any ideas? 

A:

there is probably a nice way to do that with regular expression, but I am more comfortable with reshape so here is a solution with that. 
Also I am not sure of your way of reading your file (your read bit by bit but then you cast it into a uint8).  Why not directly reading uint8 ? 
So the sequence is: read all the bytes (uint8) in the file, convert to char with the dec2bin function, then reshape into the number of column you specify (with a litle twist to add the whitespace in the middle). 
It goes:
%% // read the file byte by byte (8 bits at a time)
fid = fopen('dice.png','r','b');
x = fread(fid,'*uint8') ;
fclose(fid);                        %// don't forget to close your file

%% //  pad the end with 0 if we don't have a multiple of nByteColumn
nByteColumn = 4 ; %// your number of columns

nByte     = length(x) ;                             %// total number of byte read from the file
nByte2pad = nByteColumn-mod(nByte,nByteColumn) ;    %// number of byte to add to get a full array
x = [x ; zeros( nByte2pad , 1 , 'uint8' ) ];        %// do the padding

%% // now convert to alphanumeric binary (=string) and reshape to your liking
S = [repmat(' ',nByte+nByte2pad,1) dec2bin(x)].' ; %'//  convert to char and add a whitespace in front of each byte/string
S = reshape( S , nByteColumn*9 , [] ).' ;           %'// reshape according to your defined number of column 
S(:,1) = [] ;                                      %// remove the leading whitespace column

You obtain a char array containing all your values shaped in the number of column you specified:
>> S(1:3,:)
ans =
10001001 01010000 01001110 01000111
00001101 00001010 00011010 00001010
00000000 00000000 00000000 00001101

