Need help importing Fortran90 binary files into MATLAB
2 views (last 30 days)
Show older comments
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155892/image.png)
instead of this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155894/image.png)
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.
0 Comments
Answers (2)
Walter Roberson
on 20 Aug 2016
The default for fread is uint8=>double . You need to specify a precision, probably '*single' or 'single=>double'
0 Comments
dawaske
on 20 Aug 2016
Edited: dawaske
on 20 Aug 2016
2 Comments
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
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".
See Also
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!