Write Matrix to File
- Suppose we have a matrix that we want to write to a file. The following code snippet will do that.
subroutine mat_w2f(mat,fname,N)
integer, intent(in) :: N
real(kind=8), intent(in), dimension(N,N) :: mat
character (len=*), intent(in) :: fname
integer :: i
open(unit=1,file=fname)
do i=1,N
do j=1,N
write(1,*), mat(i,j)
end do
end do
close(1)
end subroutine mat_w2f
It simply writes each entry of a matrix to a line in the file.
The reason for using len=*
is that we have a variable string input.
Note this is a simply hack to dump as an ASCII file. It's better to use a binary output (I use NetCDF)
- Formatted version
- Some references
Write Formatted Complex Number
- Suppose we want to output a nicely formatted complex number for output