MATLAB: Are some bytes skipped from input when I try to use FREAD function to read a file that is opened in ‘rt’ mode on Windows platforms in MATLAB 7.11 (R2010b)

binaryfopenfreadhexMATLAB

I am trying to read the contents of a binary file one byte at a time using the FREAD function. I use the following commands to open a file and then perform the read operation.
fid = fopen('myFile.dat','rt')
while ~feof(fid)
data = fread(fid,8,'uint8');
dec2hex(data)
end
fclose(fid)
Upon execution, I observe that where ever there is an input sequence 0D(in hex) followed by 0A(in hex) in the source file, the byte for '0D' is ignored in the read input.
I expect to get the data into MATLAB exactly as it is in the input file.

Best Answer

This behavior is observed on Windows platforms when you read a file in text mode using the 'rt' option in FOPEN. The following applies on Windows systems, in text mode:
- Read operations that encounter a carriage return followed by a newline character ('\r\n' or 0D 0A in Hex) remove the carriage return from the input.
- Write operations insert a carriage return before any newline character in the output.
Please refer to the documentation of FOPEN function for more information:
<http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html>