How do I best write a direct access binary file in MATLAB that can be read in by a colleague using FORTRAN90 (Visual Fortran on A Windows 7 Platfrorm)?

2 views (last 30 days)
In MATLAB, I am trying to write out to a direct access binary file, a single one-dimensional arry (dd3(793)) so that it can be read by a colleague using a FORTRAN90 program via Visual FORTRAN on a Windows 7 Platform.
To write the file in MATLAB, I use the following:
fileID3=fopen(Name.bin','w+','l');
I have also used 'w', 'a+' and 'a' for the permission and 'a' for the format
and then:
fwrite(fileID3,dd3,'double'); or 'real*8' for the format.
In the FORTRAN PROGRAM, we do the following a:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
real dd3(793)
open(unit=10, file='Name.bim','status='old',access='direct',recl=793*8)
read(10,rec=1)dd3
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
when I try to print out values for dd3, all get is garbage for the various options.
Any idea what I migh be doing wong?
Steve

Accepted Answer

Walter Roberson
Walter Roberson on 31 May 2013
In your Fortran program, you might need to specify real*8 instead of real
You might be encountering Endian issues. It appears you are writing in little-endian, which is probably right for Visual Fortran, but you might want to try big-endian.
I suggest that you construct some known values at the MATLAB level, write them out to the file, use a file dumper to verify the hex content of the file, then read them into Fortran. Inside Fortran, write the values out using Z format as well as a floating point format, and see what you get.
bytes = uint8([128 129 130 131]);
as_single = typecast(bytes,'single');
format long g
as_single
format hex
as_single
format long g
then fwrite() as_single to a file, read it at the Fortran level as real*4, display it as floating, display it as hex... Try with big-end and little-end files.
Then expand on the above for 8-byte values, class double, real*8, if you have not determined enough to be able to read numerically.
  1 Comment
Steven
Steven on 31 May 2013
Thanks, Walter....Turned out it was the neglecting to define the variable array as real*8 in the FORTRAN code that was giving me the problem...

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 31 May 2013
Edited: James Tursa on 31 May 2013
You usually do not want to use direct access for this situation. Fortran can put record markers on each record when it writes such a file, interleaving the data with the markers, so it also expects to see these markers when it reads a direct access file. Instead of direct access, what you want to do is open the file as a stream (ACCESS="STREAM"), so that the Fortran will expect only raw binary data and no record markers.
Also, as Walter already mentioned, you need to use real*8 or real(8) on the Fortran side if you are writing doubles on the MATLAB side.
If you have Endian issues, then that can be handled on either the MATLAB side or the Fortran side. I would normally just write the file out natively on the MATLAB side and then let the reader handle any Endian issues. E.g., on the Fortran side you might need to use something like CONVERT='SWAP' on the open statement if reading in raw doesn't work.
  5 Comments

Sign in to comment.

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!