簡體   English   中英

從fortran寫出256 ^ 3數組的最佳方法,所以matlab可以讀取它

[英]best way to write out 256^3 array from fortran so matlab can read it

我有一個我創建的數組,大小:256 ^ 3。

real*8, dimension(256,256,256) :: dense

open(unit=8,file=fname,form="unformatted")
write(8)dense(:,:,:)
close(8)

寫出來的最佳方式是什么,所以Matlab可以讀取它? 我有一些我想要使用的后期處理。

我正在使用gfortran所以我不能使用二進制格式:{這是真的嗎? 我將表單設置為“二進制”,但它無法識別它。 我也沒有安裝ifort。

使用未格式化的流訪問將數組寫出。 流訪問是二進制的標准等價物。 竊取IRO-bot的答案:

real(kind=kind(0.0d0)),dimension(256,256,256) :: dense

open(unit=8,file='test.dat',& ! Unformatted file, stream access
  form='unformatted',access='stream')
write(unit=8) dense           ! Write array
close(unit=8)
end

這很可能足以滿足您的需求。 但請注意,對於更復雜或復雜的輸出要求,Matlab附帶了一個可從編譯語言調用的例程庫,允許您編寫.mat文件。 還存在可以促進這種數據傳輸的其他庫 - 例如HDF5。

是的,您可以使用IanH建議的stream訪問或direct訪問來編寫二進制文件:

integer :: reclen
real(kind=kind(0.0d0)),dimension(256,256,256) :: dense

inquire(iolength=reclen)dense ! Inquire record length of the array dense
open(unit=8,file='test.dat',& ! Binary file, direct access
     form='unformatted',access='direct',recl=reclen)
write(unit=8,rec=1)dense      ! Write array into first record 
close(unit=8)

end

除非在open語句中指定access屬性,否則文件將以sequential模式打開,這可能不方便讀取,因為它為每條記錄添加了一個填充,其中包含有關記錄長度的信息。 通過使用direct訪問,您可以明確指定記錄長度,在這種情況下,寫入的文件的大小將精確為8*256^3 ,因此假設您知道數組排序和字節順序,您可以閱讀它來自您的MATLAB腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM