Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96448

Matlab -> Python. Reading heterogeneous 1D binary array from disk

Say we have a simple, small file with a 1D array holding different value types, with a specific structure (first item is MATLAB uint, second value is MATLAB uint, and the rest of values are float)

How can I read such an array of heterogeneous types from a file in Python?

The equivalent code in MATLAB is below.

function M = load_float_matrix(fileName)

fid = fopen(fileName);
if fid < 0
    error(['Error during opening the file ' fileName]);
end

rows = fread(fid, 1, 'uint');
cols = fread(fid, 1, 'uint');
data = fread(fid, inf, 'float');

M = reshape(data, [cols rows])';

fclose(fid);

end

Note: this thread describes the following approach to read three consecutive uint32 values:

f = open(...)
import array
a = array.array("L")  # L is the typecode for uint32
a.fromfile(f, 3)

but, how do I know that L is the typecode for uint32? What about the other types? (e.g. float).

Also, how can I read consecutive values from f? Would a.fromfile move the read pointer in the file forward?

Upvotes: 1

Views: 355

Answers (1)

chunyun
chunyun

Reputation: 51

Try numpy.

Below is one way of doing it.

import numpy as np
f = open(filename,"r")
N = np.fromfile(fp,dtype=np.int32,count=2)
a = np.fromfile(fp,dtype=np.float64)
a = np.resize(a,N)

with this you can also read a mixed format/type( text + binary ) file. It is possible to combine line 3 and line 4 by properly formatting the dtype option, google for more examples.

Upvotes: 3

Related Questions