2017-07-06 593 views
1

誰能告訴我如何使用fortran來讀取.mat? 我需要用fortran來處理matlab中的2d矩陣。 我使用如何在fortran中讀取.mat

open(unit = 9006, file = 'zmatrix.mat',status = 'unknown', form = 'unformatted') 
do i=1,296 
    do j=1,278 
    read(9006,*) zmatrix(i,j) 
    end do 
end do 

但據說FORTRAN運行時錯誤:本未格式化的數據傳送格式。 讚賞任何建議!

+0

我認爲你的問題有MATLAB開發人員的官方答覆:https://www.mathworks.com/help/matlab/read-and-write-matlab-mat-files-in-cc-and-fortran.html – King

回答

0

這個小程序讀取一個文件.MAT(名字是高管行第一個參數,即./a.out Jet0500。該文件包含一個二維變量,

!compile with gfortran Read_mesh.f90 

Module Mat_Read 

    real, allocatable :: omega(:,:) 
contains 

    Subroutine Read_mesh 
    implicit none 
    integer :: unitno=10 
    integer :: kx,ncolumn,nrow 
    character (len=30) :: filename 
    call getarg(1,filename) 
    open(unit=unitno,file=filename,form="formatted",& 
     &status="old",action="read") 
    do kx=1,3     ! read by 3 lines 
     read(unitno,fmt="(A1)") 
    end do 
    read(unitno,fmt="(8x,i14)")nrow 
    read(unitno,fmt="(10x,i14)")ncolumn 
    allocate (omega(nrow,ncolumn)) 
    do kx=1,nrow 
     read(unitno,*) omega(kx,:) 
    end do 
    close(unitno) 
    End Subroutine Read_mesh 
End Module Mat_Read 

Program test_Read_mesh 
    use Mat_Read 
    call Read_mesh 
    print *,omega(1,1)  
End Program test_Read_mesh 
+0

謝謝!當我運行代碼,它說是致命的錯誤:無法打開模塊文件...對於「結束模塊Mat_Read」行?我正在使用GNU Fortran – brian

+0

該程序確實編譯沒有錯誤對我來說三件事:你保存它作爲一個文件w /擴展名爲f90?複製過程中第一行(Module ..)是否被損壞?最後,這與此無關你的編譯問題,你將不得不執行例如./Read_mesh文件名,其中文件是一個.mat文件,它有一個2d變量。 –

+0

你是什麼意思,「你將不得不執行如./Read_mesh文件名,其中文件是一個.mat文件」?你的意思是我需要將文件名改爲.mat的名字嗎? – brian