|
1 | 1 | program example_savetxt |
2 | | - use stdlib_io, only: savetxt |
3 | | - implicit none |
4 | | - real :: x(3, 2) = 1 |
5 | | - call savetxt('example.dat', x) |
6 | | - call savetxt('example.csv', x, delimiter=',') |
| 2 | + use stdlib_io, only: savetxt |
| 3 | + use, intrinsic :: iso_fortran_env, only: output_unit |
| 4 | + |
| 5 | + implicit none |
| 6 | + real :: x(3, 2) |
| 7 | + integer :: unit |
| 8 | + x = reshape([1, 2, 3, 4, 5, 6], [3, 2]) |
| 9 | + |
| 10 | + ! Output to file, default options |
| 11 | + call savetxt('example.dat', x) |
| 12 | + ! Result in example.dat: |
| 13 | + ! 1.00000000E+00 4.00000000E+00 |
| 14 | + ! 2.00000000E+00 5.00000000E+00 |
| 15 | + ! 3.00000000E+00 6.00000000E+00 |
| 16 | + ! |
| 17 | + ! Output to file, with custom delimiter |
| 18 | + call savetxt('example.csv', x, delimiter=',') |
| 19 | + ! Result in example.csv: |
| 20 | + ! 1.00000000E+00, 4.00000000E+00 |
| 21 | + ! 2.00000000E+00, 5.00000000E+00 |
| 22 | + ! 3.00000000E+00, 6.00000000E+00 |
| 23 | + ! |
| 24 | + ! Output to file, with header and footer and custom comment string |
| 25 | + call savetxt('example2.dat', x, header='x (x-units) y (y-units)', comments='!#', footer='This is all data') |
| 26 | + ! Result in example2.dat: |
| 27 | + !!# x (x-units) y (y-units) |
| 28 | + ! 1.00000000E+00 4.00000000E+00 |
| 29 | + ! 2.00000000E+00 5.00000000E+00 |
| 30 | + ! 3.00000000E+00 6.00000000E+00 |
| 31 | + !!# This is all data |
| 32 | + ! |
| 33 | + ! Output to file. Custom format |
| 34 | + call savetxt('example3.dat', x, fmt='f4.2') |
| 35 | + ! Result in example3.dat: |
| 36 | + ! 1.00 4.00 |
| 37 | + ! 2.00 5.00 |
| 38 | + ! 3.00 6.00 |
| 39 | + ! |
| 40 | + ! Output to standard output. Diferent formats for the columns |
| 41 | + call savetxt(output_unit, x, header='x y', fmt='(f4.2,1x,f3.1)') |
| 42 | + ! Output: |
| 43 | + ! # x y |
| 44 | + ! 1.00 4.0 |
| 45 | + ! 2.00 5.0 |
| 46 | + ! 3.00 6.0 |
| 47 | + ! |
| 48 | + ! Save data and then overwrite it: |
| 49 | + call savetxt('example4.dat', x) |
| 50 | + call savetxt('example4.dat', 2 * x) |
| 51 | + ! Result in example4.dat: |
| 52 | + ! 2.00000000E+00 8.00000000E+00 |
| 53 | + ! 4.00000000E+00 1.00000000E+01 |
| 54 | + ! 6.00000000E+00 1.20000000E+01 |
| 55 | + |
| 56 | + ! Save data and then append some more: |
| 57 | + open (newunit=unit, file='example5.dat') |
| 58 | + call savetxt(unit, x, header='x (x-units) y (y-units)') |
| 59 | + call savetxt(unit, 2 * x) |
| 60 | + close (unit) |
| 61 | + ! Result in example5.dat: |
| 62 | + ! # x (x-units) y (y-units) |
| 63 | + ! 1.00000000E+00 4.00000000E+00 |
| 64 | + ! 2.00000000E+00 5.00000000E+00 |
| 65 | + ! 3.00000000E+00 6.00000000E+00 |
| 66 | + ! 2.00000000E+00 8.00000000E+00 |
| 67 | + ! 4.00000000E+00 1.00000000E+01 |
| 68 | + ! 6.00000000E+00 1.20000000E+01 |
| 69 | + |
7 | 70 | end program example_savetxt |
0 commit comments