0

I wanted to write a subroutine in modern fortran (90 or newer) to write the elements of a given matrix into a file output.txt. Each line of the file output.txt contains the elements of each row of the matrix (the natural).

The following is an example (of course, it is not a subrutine depending on a matrix A, is a function fix the matrix defined inside just to explain you my questions).

  PROGRAM testing
  !
  IMPLICIT NONE
  !
  REAL, ALLOCATABLE :: A(:,:)
  INTEGER           :: i, j, n, m
  CHARACTER(LEN=20) :: opt
  !
  m = 3
  n = 4
  !
  ALLOCATE( A(n,m))
  A(1,:) = (/  1 ,  2 ,  3 ,  4 /)
  A(2,:) = (/ -1 , -2 , -3 , -4 /)
  A(3,:) = (/ 10 , 20 , 30 , 40 /)
  !
  !
  opt = '(4(e25.15,1x))' ! <--- I NEED THE VALUE OF M INSTEAD OF 3 HERE !!!!!!
  OPEN(1, file='output.txt', status='replace', action='write')
  DO i = 1,m
     WRITE(1,opt) ( A(i,j) , j=1,n )
  END DO
  CLOSE(1)
  !
END PROGRAM testing

My problem is that "4" in the opt variable. I wanted to have define

 opt = '(4(e25.15,1x))'

but depending on n (the number of columns of the matrix):

 opt = '(n(e25.15,1x))'

Y tried to do a cast, something like:

    character :: aux(44)
    integer   :: i

    i = 3

    write (aux, "(A5,I40)") '(', i
    opt = '('//TRIM(aux)//'(e25.15,1x))'

but this depends on the 40. If the number of columns is too big this going to fail.

Do you know how to do this? Maybe there are a better option to write the matrix into an output file.

user106306
  • 315
  • 2
  • 10
  • 1
    Please use tag [tag:fortran] for all Fortran wuestions. Do not use tag [tag:fortran90] if you allow more recent ones. – Vladimir F Героям слава Mar 18 '21 at 06:43
  • 4
    Does this answer your question? [Format string for output dependent on a variable](https://stackoverflow.com/questions/9881186/format-string-for-output-dependent-on-a-variable) Ignore the accepted answer. – Vladimir F Героям слава Mar 18 '21 at 06:48
  • 1
    Also https://stackoverflow.com/questions/43482939/write-dynamical-arrays-in-fortran-90 https://stackoverflow.com/questions/27728502/dynamic-output-format-setting – Vladimir F Героям слава Mar 18 '21 at 06:49
  • 2
    Your rows and columns look wrong. (Fortran is column major), so your allocate and 4 versus 3 and M and N need to account for column major. – Holmz Mar 18 '21 at 09:02
  • 2
    That maybe true @veryreverie , however the allocate seems to imply 4 rows and 3 columns... unless I am reading it wrong? – Holmz Mar 18 '21 at 09:18
  • 1
    "but this depends on the 40". As the 40 is the field width for the repeat count you desire, to have "too many columns" you would need a number of columns with more than 40 digits. That's going to cause problems long before it comes to worrying about whether your file is formatted correctly. (That would also try to write 45 characters to a character of length 44.) – francescalus Mar 18 '21 at 09:26
  • Thanks!! The solution in the other questions that you suggest me solved my problem. – user106306 Mar 18 '21 at 13:41
  • 1
    [Does this answer](https://stackoverflow.com/a/66685341/380384) help you in formatting a matrix for text output in columns? – John Alexiou Mar 18 '21 at 15:37

0 Answers0