Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
94 Views

Export outputs to csv file

Im new with programming

According to this code i want to export my output to csv file. The problem is i prefer to export every 100 number in each row which have totally 10 row, but it export all numbers in one column. How can i fix it?

With regard

 program Console5
    implicit none 
    
    integer::  i,j
    real rand
    real, dimension (100):: a
   CALL RANDOM_SEED	
   
    open(unit=1,file='test.csv',status='unknown')
    do i=1,10     
    do j=1,100
    call random_number(rand)
    a(j)=(rand*1)
     write(1,*) a(j)
    end do   
    end do
    
  end program

 

0 Kudos
7 Replies
Highlighted
New Contributor II
94 Views

For completeness, I can think of the following approaches for writing a 10x100 table:

(a) Write elements individually

program p1
  integer :: i, j
  real :: a
  open(unit=1,file='test.csv',status='unknown')
  do i = 1,10
    do j = 1,100
      ! compute next element
      call random_number(a)
      ! write element (no linebreak)
      write(1,'(g14.7,x)',advance='no') a
    end do
    write(1,*)  ! newline
  end do
  close(1)
end program

(b) Write line by line

program p2
  integer :: i
  real, dimension(100) :: a
  open(unit=1,file='test.csv',status='unknown')
  do i = 1,10
    ! compute next line
    call random_number(a)
    ! write line
    write(1,'(100(g14.7,x))') a
  end do
  close(1)
end program

(c) Write entire table at once

program p3
  real, dimension (100,10):: a
  ! precompute full table
  call random_number(a)
  ! write table
  open(unit=1,file='test.csv',status='unknown')
  write(1,'(100(g14.7,x))') a
  close(1)
end program

If the table is not too large, I'd prefer the last variant (c) for simplicity and performance reasons. It also mandates strict separation of computation and IO. However, the first program (a) is the most flexible in that it can handle variable numbers of elements per line, alternating output formats, etc.

Note that all variants require formatted output, and I used the format 'g14.7' just as an example.

0 Kudos
Highlighted
Valued Contributor III
94 Views

The example above are 'space' separated nor comma separated. If you want comma I would use:

write(1,'(100(g14.7,:,","))') a 

or

write(1,'(*(g14.7,:,","))') a 

The : (colon) terminates the format if there are no more items, thus stopping a trailing comma being written.  

0 Kudos
Highlighted
Black Belt Retired Employee
94 Views

Consider using G0.7 instead of G14.7.

0 Kudos
Highlighted
New Contributor II
94 Views

Andrew,

can you comment on the asterisk in your second format? I remember from experimenting with it that it behaves like an infinite repeat count, but couldn't find it documented anywhere in the standard or in the Intel compiler documentation.

Kind regards, Ferdinand
 

0 Kudos
Highlighted
Valued Contributor III
94 Views

NOTE 10.7 The effect of an unlimited-format-item is as if its enclosed list were preceded by a very large repeat count. There is no file positioning implied by unlimited-format-item reversion. This may be used to write what is commonly called a comma separated value record. For example, WRITE( 10, ’( "IARRAY =", *( I0, :, ","))’) IARRAY produces a single record with a header and a comma separated list of integer values.

 

https://j3-fortran.org/doc/year/10/10-007r1.pdf ;

0 Kudos
Highlighted
New Contributor II
94 Views

Thank you! A search for "unlimited format item" also brings up one of Steve's Dr. Fortran articles dealing, in part, with the question aked by the OP.

0 Kudos
Highlighted
Valued Contributor III
94 Views

On the subject of comma separated value it is better to define a separator character that can be set by the software or user. Comma is generally OK in the USA and UK but in many places it is used as a decimal separator and semi-colon ";" is often used instead.  This can be the source of many problems if not considered properly.

0 Kudos