MATLAB to Python fread

2019-09-14 21:50发布

What I am basically trying to do is convert from some MATLAB code to Python:

The MATLAB code:

    for time = 100:model_times
        for i = 1:5
            indat = fread(fid,[48,40],'real*4');
            vort(:,:,i,time) = indat';
        end
    end

fid holds the file path (a DAT file) is being used. vort is a preallocated as: vort = zeros(40,48,5,model_times). model_times is a fixed integer (e.g. 100).

What seems to be happening is that the .dat file data is being read in as a 48x40 matrix, then inserted into the preallocated array vort, at a fixed i and time (the loop counters).

I have attempted this in Python:

    for time in range(model_times):
        for i in range(5):
            vort[:,:,i,time] = np.fromfile(fid,np.float64)

I receive an error that says, "ValueError: operands could not be broadcast together with shapes (40,48) (6048000)". The error occurs on the last line of Python code above. I have also tried adding .reshape((40,48,5,model_times)) to the line with the error, but receive another error that says "ValueError: total size of new array must be unchanged."

So my question is, what is the Python equivalent to MATLAB's "fread", that can handle multidimensional arrays?

On a scale from 1 to 10, with 1 being a total beginner and 10 being a seasoned veteran, I'm about a 4.

2条回答
别忘想泡老子
2楼-- · 2019-09-14 22:12

I'm assuming you are reading 48*40 elements from a binary file at each iteration, each element being 8 bytes in size. Your code is breaking because np.fromfile reads every element by default. To fix this try

vort = np.zeros( (48,40,5,model_times) )
for time in xrange(model_times):
  for i in xrange(5):
    dat = np.fromfile(fid, dtype=np.float64,count = 48*40)
    vort(:,:,i,time) = dat.reshape((48,40))
查看更多
forever°为你锁心
3楼-- · 2019-09-14 22:15

This should work too. No reason you can't do it all in a single read:

vort = np.fromfile(fid, np.float64).reshape((model_times,5,48,40)).transpose()

You have to be careful to reshape the 1-D array into the native order of the array indices in the file (model_times,5,48,40), then use transpose to reorder the indices to what you want (40,48,5,model_times). If you tried to reshape directly to the latter, you'd get data in the wrong places.

查看更多
登录 后发表回答