Pregunta

I want to write a matrix with a lot of data to a file row by row. For example, I have a matrix 100*100 and I want to have it in form 100*100 in the file. However, it doesn't work.Following is my code and some description. N and M are integers around some hundreds. RECL is expected length I set the file but here it seems this command does not work. The output is with 198 lines when N is set 99 and M is set 200. Vec is a double precision complex matrix. How could I output the values of Vec keeping its original format N*M? My compile command is "ifort -o out test.f90".

open(unit=2, file='graph1.txt', ACTION="write", STATUS="replace",RECL=40*M+10)
do i=1,N
 do j=1,M
  write(2, '(F)', advance='no') real(Vec(i,j)) 
 end do
  write(2, *) '' 
end do

Following @george advice, I coded a program like this:

program test
implicit none

integer i,j

open(unit=2, file='graph1.txt', ACTION="write", STATUS="replace")
do i=1,500
 write(2, '(1600F14.7)')( 0.00001 ,j=1,499)
end do

close(2)

end

With this code, problem solved! Maybe I didn't compile correctly last time.

¿Fue útil?

Solución

expanding on my comment, you should also use an implicit loop..try this:

open(unit=2, file='graph1.txt', ACTION="write", STATUS="replace")
do i=1,N
     write(2, '(1000F14.7)')( real(Vec(i,j)) ,j=1,M)
end do

or for sufficiently modern compilers (I'm not sure how new.. )

     write(2, '(*(F14.7))')( real(Vec(i,j)) ,j=1,M)

Note as has been pointed out, the parenthesis around (F14.7) are required for the * unlimited-format-item in the 2008 standard.

may as well pull in the other comments, you can also do this:

      write(2, '(*(F14.7))')real(Vec(i,:M))

Otros consejos

ifort uses a default record length of 80. Everything beyond that is put on the next line. You can extend that at runtime by issueing export FORT_FMT_RECL=250, which extends that to 250 characters. (You need to adjust that number, of course).

But my guess would be the format specifier of your write statement. Did you try writing the matrix row-by-row instead of element-wise? Then you could directly specify the number of elements (instead of using advance='no').

This post might be helpful as well!

EDIT: Writing row-by-row could be realized like this:

open(unit=2, file='graph1.txt', ACTION="write", STATUS="replace")
do i=1,N
  write(2,*) real( Vec(i,:) ) 
end do
close(2)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top