How do I read a FORTRAN unformatted binary data file into MATLAB?
Show older comments
Below is a data file generated by a FORTRAN program. The following is the hexadecimal dump of the FORTRAN binary data file:
0000000 0000 0048 4920 616d 2061 2067 656e 6975
0000020 7320 6772 6f75 6e64 686f 6721 2020 5275
0000040 6e21 2020 5275 6e21 2020 5275 6e21 2020
0000060 2020 2020 2020 2020 2020 2020 2020 2020
0000100 2020 2020 2020 2020 2020 2020 0000 0048
0000120 0000 000c 3f8c cccd 400c cccd 4053 3333
0000140 0000 000c 0000 0008 0000 0004 0000 0005
0000160 0000 0008
A similar data file from C (flat binary file) is attached to this solution. Below is the code that was used to generate the binary data file.
program wrt_bin
character*72 name
real*4 xsource, zsource, dt
integer*4 ntrace, nt
name='I am a genius groundhog! Run! Run! Run!'
xsource=1.1
zsource=2.2
dt=3.3
ntrace=4
nt=5
open(unit=11, file='wrt_bin.bin', form='unformatted',
+ status='new')
WRITE(11) NAME
WRITE(11) XSOURCE,ZSOURCE,DT
WRITE(11) NTRACE,NT
close(11)
end
Here is the C code that was used to generate the flat binary data file:
#include<stdio.h>
#include<stdlib.h>
void main()
{
char name[72];
int ntrace, nt;
double xsource, zsource, dt;
FILE *fp;
sprintf(name," I am a genius groundhog! Run! Run! Run!");
xsource=1.1;
zsource=2.2;
dt=3.3;
ntrace=4;
nt=5;
fp=fopen("wrt_binc.bin", "wb");
fwrite(name, sizeof(char), 72, fp);
fwrite(&xsource, sizeof(double), 1, fp);
fwrite(&zsource, sizeof(double), 1, fp);
fwrite(&dt, sizeof(double), 1, fp);
fwrite(&ntrace, sizeof(int), 1, fp);
fwrite(&nt, sizeof(int), 1, fp);
fclose(fp);
}
Accepted Answer
More Answers (0)
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!