Need help importing Fortran90 binary files into MATLAB
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Walter Roberson
2016-8-20
The default for fread is uint8=>double . You need to specify a precision, probably '*single' or 'single=>double'
0 个评论
dawaske
2016-8-20
编辑:dawaske
2016-8-20
2 个评论
Walter Roberson
2016-8-21
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
2016-8-22
编辑:James Tursa
2016-8-22
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".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!