-1

I have a binary file (>1GB in size) which contains single precision data, created in Matlab.

I am new to Python and would like to read the same file structure in Python.

any help would be much appreciated:

From Matlab, I can load the file as follow:

fid = fopen('file.dat','r');

my_data = fread(fid,[117276,1794],'single');

Many thanks

InP

Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48

1 Answers1

1

Using numpy is easiest with fromfile https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html:

np.fromfile('file.dat', dtype=np.dtype('single')).reshape((117276, 1794))

where np.dtype('single') is the same as np.dtype('float32')

Note that it may be transposed from what you want since MATLAB reads in column order, while numpy reshapes with row-order.

Also, I'm assuming that using numpy is ok since you are coming from MATLAB and probably will end up using it if you want to keep having MATLAB-like functions and not have to deal with pure python like these answers Reading binary file and looping over each byte

Scott Staniewicz
  • 712
  • 6
  • 14
  • Does Matlab add a header or other overhead to the file? Multiplying 117276*1794*4 doesn't reach 1GB. – Mark Ransom Dec 17 '18 at 23:37
  • Hi Mark, that was mistyping, the actual size is 158616 x 1794 and the file size is 1,138,228,416 bytes. This is 4 times the file dimensions. – Ahmed Athab Dec 18 '18 at 19:00