Matlab incorrectly reading a binary file

3 visualizaciones (últimos 30 días)
Greg Heinlein
Greg Heinlein el 7 de Mayo de 2018
Comentada: Greg Heinlein el 8 de Mayo de 2018
I need MatLab to read a binary file that has a specific format. The file was generated with Fortran and I know the exact format that the file takes. It has 12 integers in the header that describes the length of the rest of the file, which is double precision numbers. I can read the file with a Fortran code so I know the file has the correct values. I am having two issues with the code. The first is when I try to read the file there are 2 extraneous integer values that should not be there and do not show up when I read the file with Fortran. The second issue is when I try to read the double precision numbers none of the values are correct (ranging between the limits of the exponent +-308). The following is the bit of code I am trying to get working:
_ _fid=fopen('history.hs','r');
header_pe=fread(fid,14,'int32','ieee-be');
ct_i=abs(header_pe(8)-header_pe(7))+1;
ct_j=abs(header_pe(10)-header_pe(9))+1;
ct_k=abs(header_pe(12)-header_pe(11))+1;
ct_time=header_pe(3)*header_pe(2)-1;
fseek(fid,14*4,'bof');
x=fread(fid,[ct_i ct_j],'real*8','ieee-be');__
Any insight into why this would be happening are greatly appreciated.

Respuestas (1)

James Tursa
James Tursa el 7 de Mayo de 2018
Please show the Fortran code that was used to write the file, and also the Fortran code that correctly reads the file. Depending on the Fortran options you use, there can be extra bytes inserted in the binary file that MATLAB would have to account for. Those may be the "2 extraneous integer values" that you are referring to ... they really are in the file and the Fortran write statement put them there silently.
The exponent +-308 stuff often indicates that either a shift in where you need to start the reading, or that the bytes are swapped around (e.g. the file came from a different machine). The latter can often be fixed by using the swapbytes() function.
  1 comentario
Greg Heinlein
Greg Heinlein el 8 de Mayo de 2018
This is the basic fortran code used to read the file. The write code is basically the same except that since it writes in big endian the only change is that the reads are changed to writes and the convert in the open statement is excluded. The constants used in the read statements below were set in the code (i.e. nj, nk, tmax, etc.) I just excluded them for brevity.
integer :: j,k,nj,nk
integer :: ct_pln,blkn,pmi,max_plns
integer :: istr,ien,jstr,jen,kstr,ken
integer :: id,nprt,ntc,cti,ctj,ctk,t,tmax
real*8,allocatable,dimension(:,:,:) :: x,y,z,ax,ay,az
real*8,allocatable,dimension(:,:,:,:) :: p,force
open(unit=id,file=name1,form='unformatted',convert='big_endian')
read(id)nprt,ntc,ct_pln,max_plns,blkn,istr,ien,jstr,jen,kstr,ken,pmi
read(id) ((x(1,j,k),j=1,nj),k=1,nk), &
((y(1,j,k),j=1,nj),k=1,nk), &
((z(1,j,k),j=1,nj),k=1,nk), &
((ax(1,j,k),j=1,nj-1),k=1,nk-1), &
((ay(1,j,k),j=1,nj-1),k=1,nk-1), &
((az(1,j,k),j=1,nj-1),k=1,nk-1)
do t=1,tmax
read(id) ((p(1,j,k,t),j=1,nj-1),k=1,nk-1)
enddo
close(id)

Iniciar sesión para comentar.

Categorías

Más información sobre Fortran with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by