Matlab incorrectly reading a binary file
3 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
James Tursa
2018 年 5 月 7 日
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.
参考
カテゴリ
Help Center および File Exchange で Fortran with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!