I have practically no knowledge of Matlab, and need to translate some parsing routines into Python. They are for large files, that are themselves divided into \'blocks\', an
When replacing Matlab with Python, I wanted to read binary data into a numpy.array, so I used numpy.fromfile to read the data into a 1-dimensional array:
import numpy as np
with open(inputfilename, 'rb') as fid:
data_array = np.fromfile(fid, np.int16)
Some advantages of using numpy.fromfile
versus other Python solutions include:
count=
argument, but it defaults to -1
which indicates reading the entire file.Being able to specify either an open file object (as I did above with fid
) or you can specify a filename. I prefer using an open file object, but if you wanted to use a filename, you could replace the two lines above with:
data_array = numpy.fromfile(inputfilename, numpy.int16)
Matlab's fread
has the ability to read the data into a matrix of form [m, n]
instead of just reading it into a column vector. For instance, to read data into a matrix with 2 rows use:
fid = fopen(inputfilename, 'r');
data_array = fread(fid, [2, inf], 'int16');
fclose(fid);
You can handle this scenario in Python using Numpy's shape
and transpose
.
import numpy as np
with open(inputfilename, 'rb') as fid:
data_array = np.fromfile(fid, np.int16).reshape((-1, 2)).T
-1
tells numpy.reshape to infer the length of the array for that dimension based on the other dimension—the equivalent of Matlab's inf
infinity representation..T
transposes the array so that it is a 2-dimensional array with the first dimension—the axis—having a length of 2.Really though, I want to know how to replicate
[A, count] = fread(fid, 3, 'uint32');
In Matlab, one of fread()
's signatures is fread(fileID, sizeA, precision)
. This reads in the first sizeA
elements (not bytes) of a file, each of a size sufficient for precision
. In this case, since you're reading in uint32
, each element is of size 32 bits, or 4 bytes.
So, instead, try io.readline(12)
to get the first 3 4-byte elements from the file.
From the documentation of fread, it is a function to read binary data. The second argument specifies the size of the output vector, the third one the size/type of the items read.
In order to recreate this in Python, you can use the array module:
f = open(...)
import array
a = array.array("L") # L is the typecode for uint32
a.fromfile(f, 3)
This will read read three uint32 values from the file f
, which are available in a
afterwards. From the documentation of fromfile:
Read n items (as machine values) from the file object f and append them to the end of the array. If less than n items are available, EOFError is raised, but the items that were available are still inserted into the array. f must be a real built-in file object; something else with a read() method won’t do.
Arrays implement the sequence protocol and therefore support the same operations as lists, but you can also use the .tolist()
method to create a normal list from the array.
The first part is covered by Torsten's answer... you're going to need array
or numarray
to do anything with this data anyway.
As for the %08X and the hex2dec stuff, %08X is just the print format for those unit32 numbers (8 digit hex, exactly the same as Python), and hex2dec('4D445254') is matlab for 0x4D445254.
Finally, ~= in matlab is a bitwise compare; use == in Python.