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)?

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

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

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)

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

Confusingly, in Fortran, ACCESS="DIRECT" might not use any record markers at all, depending on the underlying implementation. If the DD statement of your JCL specifies fixed length blocks, then the RECL can be enough to determine the disk position. Unless, of course, you selected PACKED records.
I think I better quit there, as I seem to be in danger of mentally reconstructing IBM mainframe control languages. (If I start remembering the differences between JESS2 and JESS3, then run for the hills!)
Thanks james, as I memtioned to Walter, found that the problem was indeed in not using real*8 on the FORTRAN side...Thanks for the help
Now how do I shut off that MemoryCrawler (TM) that is digging around in my mental basement for that JESS2 vs JESS3 information???
Fight fire with fire ... try to think of something even more ancient ...
The crawler is finding the JES vs JES2 information more readily. JES3 wasn't yet common in my day.
Ah, now the MemoryCrawler can semi-rest... http://en.wikipedia.org/wiki/Job_Entry_Subsystem_2/3

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!