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!