- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Consider using G0.7 instead of G14.7.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page