Write binary file in Matlab

6 ビュー (過去 30 日間)
Haoliang Yu
Haoliang Yu 2019 年 9 月 13 日
コメント済み: Image Analyst 2019 年 9 月 14 日
Hello,
I have a binary data generated by Fortran, I am able to read my data:
fid = fopen('inflow.data0000');
sep1=fread(fid,1,'float');
nynzitime=fread(fid,3,'int32');
sep=fread(fid,1,'int32');
RAW_read = fread(fid, 3*n1*n2,'float32');
RAW = reshape(RAW_read, [n1,n2,3]);
fclose(fid);
I need to make some changes in the data and then write it out as a binary file for Fortran.
so I did this:
fileID = fopen('newinflow.data0000')
fwrite(fileID,sep1,'single');
fwrite(fileID,nynzitime,'int32');
fwrite(fileID,sep,'int32');
fwrite(fileID,RAW_read,'float32');
fclose(fileID);
then when Fortran reads the data:
namel='newinflow.data0000'
open(30,file=namel,form='unformatted')
read(30)n2al,n3l
read(30)qal(0:n2al+1,1:n3l,1:3)
close(30)
error message shows "corrupted file". I realized that it is a header/format related issue, but I am not familar with binary file, tried a few different things, no luck yet.
I attached a 'good' binary data, please see if you know how to read and write it properly for Fortran.
Thank you for your time.

回答 (2 件)

James Tursa
James Tursa 2019 年 9 月 13 日
Can you read and write as “STREAM” in your FORTRAN compiler? No header stuff to worry about.
  1 件のコメント
Haoliang Yu
Haoliang Yu 2019 年 9 月 13 日
Yep, that is something I will do today.

サインインしてコメントする。


Image Analyst
Image Analyst 2019 年 9 月 13 日
Maybe try changing the ENDIAN?
  4 件のコメント
Haoliang Yu
Haoliang Yu 2019 年 9 月 13 日
Oh, I did, but forgot that part when I paste the code here...also I worked around this problem by changing the Fortran write and read code to STREAM.
Image Analyst
Image Analyst 2019 年 9 月 14 日
A paste operation shouldn't drop out characters that were copied into the clipboard. You might want to investigate that.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFortran with MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by