Home > OS >  MATLAB vs. Python Binary File Read
MATLAB vs. Python Binary File Read

Time:11-13

I have a MATLAB application that reads a .bin file and parses through the data. I am trying to convert this script from MATLAB to Python but am seeing discrepancies in the values being read.

The read function utilized in the MATLAB script is:

fname = 'file.bin';
f=fopen(fname);
data = fread(f, 100);
fclose(f);

The Python conversion I attempted is:

fname = 'file.bin'
with open(fname, mode='rb') as f:
    bytes = f.read(100)
    # Conversion for visual comparison purposes
    data = [int(hex(ord(i)),16) for i in bytes]

I would then print a side-by-side comparison of the read bytes with their index and found discrepancies between the two. I have confirmed that the values read in Python are correct by executing $ hexdump -n 100 -C file.bin and by viewing the file's contents on the application HexEdit.

I would appreciate any insight into the source of discrepancies between the two programs and how I may be able to resolve it.

Note: I am trying to only utilize built-in Python libraries to resolve this issue.

CodePudding user response:

python automatically transforms single bytes into unsigned integers, as done by matlab, so you just need to do the following.

fname = 'file.bin'
with open(fname, mode='rb') as f:
    bytes_arr = f.read(100)
    # Conversion for visual comparison purposes
    data = [x for x in bytes_arr]
print(data)

also welcome to python, bytes is a built-in type, so please don't override the built-in bytes type ... or you'll run into unexpected problems.

Edit: as pointed by @juanpa.arrivillaga you could use the faster

fname = 'file.bin'
with open(fname, mode='rb') as f:
    bytes_arr = f.read(100)
    # Conversion for visual comparison purposes
    data = list(bytes_arr)

CodePudding user response:

An exact translation of the MATLAB code, using NumPy, would be:

data = np.frombuffer(f.read(100), dtype=np.uint8).astype(np.float64)
  • Related