這個小程序讀取一個文件.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
我認爲你的問題有MATLAB開發人員的官方答覆:https://www.mathworks.com/help/matlab/read-and-write-matlab-mat-files-in-cc-and-fortran.html – King