MATLAB: How to read a FORTRAN generated unformatted binary file in Matlab

binaryfile i/ofile inputfortranunformatted

Hello everyone.
I am trying to read a FORTRAN90 generated unformatted binary file in Matlab.
I have searched the forum and seems like my file is a bit different from the answers in the forum.
following is the first 60 lines of my binary file which I got from 'hexdump file'.
Could someone help me with this?
Thank you so much!
0000000 000e 0000 2e38 2e36 2033 0050 0000 0002
0000010 0000 000e 0000 00f0 0000 0003 0000 ee26
0000020 0133 0000 0000 000b 0000 0001 0000 0018
0000030 0000 0018 0000 0018 0000 0001 0000 0001
0000040 0000 0001 0000 0001 0000 0030 0000 0001
0000050 0000 0001 0000 0001 0000 0000 0000 0000
0000060 0000 0000 0000 8000 4046 0000 0000 8000
0000070 4046 0000 0000 0000 0000 0000 0000 8000
0000080 4046 0000 0000 0000 0000 0000 0000 0000
0000090 0000 0000 0000 0000 0000 3f07 7832 3c51
00000a0 400e 0000 0000 0000 0000 0000 0000 0000
00000b0 0000 0000 0000 0000 0000 3f07 7832 3c51
00000c0 400e 0000 0000 0000 0000 0000 0000 0000
00000d0 0000 0000 0000 0000 0000 3f07 7832 3c51
00000e0 400e 0000 0000 0000 0000 0000 0000 0000
00000f0 0000 147b 47ae 7ae1 3f84 0000 0000 0001
0000100 0000 0001 0000 0003 0000 00f0 0000 0c54
0000110 0000 0002 0000 0003 0000 0178 0000 0001
0000120 0000 0001 0000 0001 0000 0001 0000 0001
*
00001e0 0000 0001 0000 0000 0000 0000 0000 0000
*
0000200 0000 0001 0000 ffff ffff 0000 0000 0000
0000210 0000 0000 0000 ffff ffff 0000 0000 0000
0000220 0000 0000 0000 ffff ffff ffff ffff 0000
0000230 0000 0000 0000 0000 0000 0001 0000 0000
0000240 0000 0000 0000 0000 0000 ffff ffff 0001
0000250 0000 0000 0000 0000 0000 0000 0000 ffff
0000260 ffff 0000 0000 0000 0000 0000 0000 0001
0000270 0000 ffff ffff 0000 0000 0000 0000 0000
*
0000290 0000 0001 0000 0001 0000 0000 0000 0000
00002a0 0000 0000 0000 0001 0000 0000 0000 0000
00002b0 0000 0000 0000 ffff ffff 0001 0000 0000
00002c0 0000 0000 0000 0000 0000 ffff ffff 0000
00002d0 0000 0000 0000 0000 0000 ffff ffff ffff

Best Answer

fid = fopen('output_DEN.bin', 'r', 'ieee-le');
for recnum = 1 : 7
%skip header
%record 1 is a mix of char and integer
%record 2 starts with integer, might contain something else
%record 3 is integer
%record 4 is double precision
%record 5 is integer
%record 6 is character and perhaps something else
%record 7 is double precision
rs = fread(fid, [1 1], '*uint32');
rec = fread(fid, [1 rs], '*uint8');
rs2 = fread(fid, [1 1], '*uint32');
end
fclose(fid);
data = typecast(rec, 'double');
datalen = length(data);
edgelen = round(datalen .^ (1/3));
if edgelen^3 == datalen
data = reshape(data, edgelen, edgelen, edgelen);
fprintf('Found data cube!\n');
else
fprintf('Does not appear to be cube of data, left as vector\n');
end
fprintf('... done\n');