1
votes

in my fortran code i am outputting the results into a binary file.

      open(21,file=anum('press',itime),form=format_mode)
  write(21) rtime,itime,dt,nx0,ny0,nz,deltax,deltay,rlenz
  write(21) rw
  close(21)

the above is the fortran code that writes and saves the file.

i now want to open and analyse it in matlab:

fid = open('press.420000');
A = fread(fid);
close(fid);

this however, only creates a 1d array which i am guessing includes all the header information too.

i want Matlab to read the header values but not include them into the final array. i intend to reshape the array in to a 3d array as the data is from a cfd simulation which has a grid of 256x512x390 = 51,180,80

the Matlab code gives me a 1d array of 411,343,976, which cannot be correct.

thus i am struggling how to read the binary file. I need some guidance on how i should code a Matlab script to read the binary file

1
I recommend changing the Fortran to write a text file, and have Matlab read the text file.Jack
If matlab does have a binary file read capability, it would be for Fortran stream files open(access='stream'...)tim18
What is the value of format_mode? That is a very important information.Vladimir F
format_mode must be unformatted else the write would fail. My advice is to use access=stream instead of coaxing matlab to read the arcane fortran default sequential access format. The other concern is of course the variable types need to be the same in both programs.agentp

1 Answers

1
votes

You can read data in byte vector:

bytevec = fread(fid, inf, 'uint8');

Then you can look at and manually arrange elements by their indices, for example - single precision (float) data:

vec = typecast(bytevec(i1:i2), 'single');

And then convert it to default matlab double type without changing data values:

vec = cast(vec, 'double');

Finally, you can reshape raw vector to 3d matrix:

M = reshape(vec, [d1, d2, d3]);