Writing binp file with Python, Reading with Matlab

37 views Asked by At

I am writing a .binp file in Pyton and am trying to read it in Matlab again.

The .binp file has an X-Header with 11 parameters and the Y-Header has 7 parameters

#Data
X[0,0] = 0 
X[0,1] = -delta_width/2 
X[0,2] = -delta_height/2 
X[0,3] = 0.0 
X[0,4] = delta_width/2 
X[0,5] = 0.0 
X[0,6] = 0.0 
X[0,7] = 0.0 
X[0,8] = delta_height/2 
X[0,9] = 0.0 
X[0,10] = n_nvector_facet 

Y = np.zeros((n_facet*n_nvector_facet,7)) # only one normal vector per facet
Y[0,0] = -delta_width/2 
Y[0,1] = -delta_height/2 
Y[0,2] = 0.0 
Y[0,3] = 0.0 
Y[0,4] = 0.0 
Y[0,5] = 1.0 
Y[0,6] = delta_width*delta_height

#Writing to .binp
np.uint32(X[0,0]).tofile(f)
np.float32(X[0,1:9]).tofile(f)
np.uint32(X[0,10]).tofile(f)
np.float32(Y[0,0:6]).tofile(f)

Matlab Code - Read file

fid = fopen(filepath);
X1(1,1) = fread(fid, 1, 'int32');
X2(1,1:9) = fread(fid, 9, 'float32'); 
X3(1,1) = fread(fid, 1, 'uint32'); 

n = nfacetrays(1,1)*6;
    
Y{1} = fread(fid, n, 'float32');

In the read file a value is missing

  • Written X-Header: 0 -2.5 -1.5 0 2.5 0 0 0 1.5 0 1
  • Written Y-Header: -2.5 -1.5 0 0 0 1 15
  • Read X-Header: 0 -2.5000000 -1.5000000 0 2.5000000 0 0 0 1.5000000 1.4012985e-45 3223322624
  • Read Y-Header: -1.50000000000000 0 0 0 1 0 2.50000000000000

So basicaly in the X-header 0 and 1 or/and in the Y-header -2.5 are read incorrectly or one of them is missing because in the read Y-Header it starts with -1.5 instead of -2.5.

Any ideas? Where this mistake is coming from?

1

There are 1 answers

0
Warren Weckesser On BEST ANSWER

Change

np.float32(X[0,1:9]).tofile(f)

to

np.float32(X[0,1:10]).tofile(f)

The upper limit of the : is not inclusive.