How do I figure out how many bytes there are in one line of FORTRAN unformatted binary file

3 views (last 30 days)
I have the FORTRAN source code that reads in a binary file, but since I don't hardly know any FORTRAN I want to instead read in this binary file with MATLAB
one of the many FORTRAN code snippets I am "translating" is
open (unit=82, status='old', form='unformatted',
& file= filename)
read(82) var1, var2, var3, var4
read(82) var5
which i'm thinking would translate to something like:
fid=fopen(filename);
data =fread(fid,N); % where N is the number of bytes in the first "line" of the binary file
data(1) = var1;
data(2) = var2;
data(3) = var3;
data(4) = var4;
data = fread(fid,N2) % where N2 is the number of bytes in the second "line" of the binary file
var5 = data
first off, I don't know how to extract from the binary file what should be the values for N or N2 (i tried using fgetl.m but that seemed to just spit out gobblygook)
also, if multidimentional matrices were saved by FORTRAN instead of the single-valued variables in this example I have no idea how I would be able to separate them from within the output of fread
any help?
  1 Comment
James Tursa
James Tursa on 20 Feb 2018
What are the dimensions of var1, var2, var3, var4, var5? You should be able to see that in the Fortran code.
Fortran unformatted I/O is not a simple as C/C++ or MATLAB binary I/O. Fortran can put extra stuff in the file at the beginning of "lines". So you may need your MATLAB code to skip a few bytes before you read in the data. You may have to experiment with this to see how many bytes you need to skip.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 20 Feb 2018
The byte order used is not defined for unformatted writes. It will probably be the same as the byte order of the computer used to write the data... but not necessarily (as sometimes vendors wanted compatibility with all of their products.)

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!