MATLAB: How to 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)

binaryfortran

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

Best Answer

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.