Read and process a Fortran90 binary file
古いコメントを表示
Hi,
I have a Fortran90 code where a variable called 'stream' is defined such that it stores certain variable every nstep:
open(19,file='stream',form='UNFORMATTED',position='append')
if(mod(NSTEP,100).eq.0)then
write(19)NSTEP,U,W,zeta
endif
How can I properly read the file in Matlab for postprocessing?
FYI, the fortran code is built with double precision.
Thank you
3 件のコメント
James Tursa
2024 年 11 月 21 日
編集済み: James Tursa
2024 年 11 月 21 日
Unformatted Fortran can contain beginning and end of line markers in the file. You need to give us a small sample file to work with to verify this, and also tell us the type and dimensions of the variables NSTEP, U, W, and zeta.
Walter Roberson
2024 年 11 月 21 日
The FORTRAN runtime system embeds the record boundaries in the data by inserting an INTEGER*4 byte count at the beginning and end of each unformatted sequential record during an unformatted sequential WRITE. The trailing byte count enables BACKSPACE to operate on records.
Carola Forlini
2024 年 11 月 21 日
採用された回答
その他の回答 (1 件)
Les Beckham
2024 年 11 月 21 日
編集済み: Les Beckham
2024 年 11 月 21 日
It would help to have a sample file to experiment with.
Nevertheless, assuming that the write call that you have shown is the only thing that writes to this file in your Fortran code, I would suggest something like this using fread and reshape:
fid = fopen('your_file_name.ext', 'rb');
A = fread(fid);
data = reshape(A, 4, numel(A)/4)';
nstep = A(:,1);
U = A(:,2);
W = A(:,3);
zeta = A(:,4);
Note that you might run into endian-ness issues if your Fortran code runs on a different architecture than your Matlab is running on. See the documentation for the machinefmt option to fread if necessary.
5 件のコメント
Walter Roberson
2024 年 11 月 21 日
form='UNFORMATTED' has record length markers before and after each record.
Walter Roberson
2024 年 11 月 21 日
If you start with fread(fid, 1, 'integer*4') and discard that, and thereafter fread() with a skip field of 8 then it should be properly positioned to fread() more.
Unfortunately we are not told the size of NSTEP, U, W, zeta
Carola Forlini
2024 年 11 月 22 日
Les Beckham
2024 年 11 月 22 日
Thanks for adding the example.
Can you clarify what you mean by this?
NSTEP is an integer = 2000
Especially the "= 2000" part. Is that just an example of a possible value? What is the size of this integer (number of bits)?
Also, are U, W, and zeta double precision (64 bit) floating point 2-d arrays with the given size?
Carola Forlini
2024 年 11 月 22 日
カテゴリ
ヘルプ センター および File Exchange で Fortran with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

