Need help importing Fortran90 binary files into MATLAB

2 views (last 30 days)
Hello everyone!
I recently started learning how to code in Fortan90 for an upcoming project. The plan is to perform simulations with a Fortran90 program (compiled with the Linux PGI compiler in case this is important), store the output of those simulations in 2D matrices and exporting them as binary files for later use in MATLAB.
For testing purposes I created the following subroutine to export the matrices:
subroutine save_data (x,y,z)
integer, intent(in):: x,y,z
integer::i
real, dimension(x,y):: temp
character(20):: filename = ''
do i=1, z
write(filename,'(a,i3.3,a)') 'output_matrix',i,'.dat'
temp=output(1:x,1:y,i)
open(unit=13, file=filename, action="write", status="replace", form="unformatted")
write(13) temp
close(unit=13)
end do
end subroutine save_data
The problem now is, despite several days of trial & error I am still not able to import the "output_matrix***.dat"-files into MATLAB so that I can work with them. I did find several questions similar to mine on MATLABCentral (such as THIS or THIS) and tried to do what was suggested there, unfortunately with no luck. So far I was only able to achieve this:
clc
clear all
close all
fid = fopen('output_matrix001.dat');
h=fread(fid,[31,16]);
which resulted in h looking like this:
instead of this:
Could you please help me with this (for more advanced users supposedly not very hard to solve) issue? I attached the "output_matrix001.dat"-file as well as an m-file with what MATLAB should extract from the "output_matrix001.dat"-file.

Answers (2)

Walter Roberson
Walter Roberson on 20 Aug 2016
The default for fread is uint8=>double . You need to specify a precision, probably '*single' or 'single=>double'

dawaske
dawaske on 20 Aug 2016
Edited: dawaske on 20 Aug 2016
Thank you for this very helpful comment! Now the numbers seem to be right (except for the one on the top left) independent of whether I use "*single' or 'single=>double' (what's the difference between those two options anyway?); however, there is still something wrong with the order:
Do you have any idea how to fix this?
EDIT
ok so playing around a little with fread I found that if I write
h=fread(fid,[16,31],'single=>double');
instead of
h=fread(fid,[31,16],'single=>double');
I get this:
which is almost what I it's supposed to look like except the matrix is transposed of course, the number in the top left is still wrong and the last column (or line in this case) is missing.
  2 Comments
Walter Roberson
Walter Roberson on 21 Aug 2016
Remember that arrays fill down the columns first, so it is common to have to specify the number of columns first and rows second and then transpose the results.
Fortran binary files are permitted to have embedded structure. Fortran does not have any standards about the representation of binary files, except that the write and read needs to be consistent for any one compiler (on any given operating system). "Stream of bytes with no internal markers" is the portable but not guaranteed by the Fortran standards. Traditionally Fortran binary files were record oriented and so needed internal information about the record length (at least).
James Tursa
James Tursa on 22 Aug 2016
Edited: James Tursa on 22 Aug 2016
Note: The new Fortran way of writing streams of bytes is the ACCESS="STREAM" option in the OPEN statement. This should hopefully be more portable that simply using FORM="UNFORMATTED".

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!