MATLAB: Read binary file 3 bytes at a time

binaryMATLAB

Hi, I am new and I discovered that the fread by default reads a binary file 1 byte at a time, how can I read the file 3 bytes at a time. Thanks

Best Answer

Try something like this. It reads in the entire file as uint8, groups them in 3 bytes, inserts an extra 0 byte in front of the group of 3, and then reinterprets the bytes as 4 byte unsigned integers. If you don't get the result you want, we may have endian issues to deal with and may need to swap bytes.
x = fread(fid,inf,'*uint8'); % read as uint8
x = reshape(x,3,[]); % groups of 3 in columns
[m,n] = size(x); % get size
x = [zeros(1,n,'uint8'); x]; % insert row of 0's at top to get 4 bytes per number
x = typecast(x(:),'uint32'); % reinterpret bytes as 4-byte unsigned integers