首页 > 解决方案 > 如何读取单列数据并写入表中?

问题描述

  1. 我的数据文件在一列中有 33 个 x 值,然后是几个 f(x) 块,它们在开头用五个整数编号,我想读取它们 x 和任何 f(x) 块并写入将它们放入一个数组(x,y)中。我试过了,但我的程序仍然有错误。

  2. 我想每次都获得一个包含两列(x,f(x))的数组,但我的困难是我在每组 f(x)值的开头都有索引。

  3. 这段代码只占用每个数据集的第一行并写入

我的代码

    Program DataRead

    Real,Allocatable    ::X(:),F(:,:),N(:,:)       !! These are dynamic arrays, you can also define them in static form but it is much better to work with dynamic ones
    Integer DataCount, NDataSet                    !! Two Numbers which you should define later.

Open (10,file='expansioncoefs.dat')                !! Open Input File

DataCount= 33       ! Data Count
NDataSet = 15789    ! total data sets/ data series

Allocate (X(DataCount))             ! Allocating dynamic arrays according to your data file.
Allocate (F(DataCount,NDataSet))
Allocate (N(NDataSet,5))

                                    ! First loop reads X values in first rows. 
Do i=1,DataCount
Read (10,*) X(i)
Enddo

Do j=1, NDataSet                    ! Loop repeats after each data serie is readed
Read (10,*)(N(j,m),m=1,5)           ! Read Those five numbers in the begining of each series.
Do k=1,DataCount                    ! Loop to read data in each series (here is 33)
Read (10,*) F(j,k)
Enddo
Enddo

 Open (20,file='Output.txt')        ! Make an output

Do i=1, DataCount                   ! Write Outputs

 write(20,100) X(i),(F(i,j),j=1,NDataSet)

Enddo


 Open (30,file='Output2.txt')       ! Another Output
                                    ! for the five numbers
 Do k=1,NDataSet

 write(30,200) k,(N(k,l),l=1,5)

 Enddo
DesiredDataSet=1 !This is the column i desired to separate the data. so i can plot it with anything i want.


Open (40,file='Output3.txt') ! my new table

Do i=1, DataCount

Write(40,100) X(i),F(i,DesiredDataSet)

Enddo

 Write (*,*) " Program runned successfully" 

 100 Format (f10.3,1x,e25.14) ! Format Output files
 200 Format (i5,5F12.6)

End Program

我的输入数据

    4.000
    4.250
    4.500
    4.750
    5.000
    5.250
    5.500
    5.750
    6.000
    6.250
    6.500
    6.750
    7.000
    7.250
    7.500
    7.750
    8.000
    8.250
    8.500
    8.750
    9.000
    9.250
    9.500
    9.750
   10.000
   10.250
   10.500
   10.750
   11.000
   11.250
   11.500
   11.750
   12.000
 0 0 0 0 0
     0.55977887068214E-01
     0.33301830925674E-01
     0.19357033058574E-01
     0.10851672673603E-01
     0.57601109719013E-02
     0.27793714306045E-02
     0.10812628240276E-02
     0.15097129824141E-03
    -0.32616649818477E-03
    -0.54065650568596E-03
    -0.60723892717492E-03
    -0.59463219747272E-03
    -0.54352144592126E-03
    -0.47743610751912E-03
    -0.40939905472145E-03
    -0.34605270924249E-03
    -0.29025099403574E-03
    -0.24269312486307E-03
    -0.20294374360102E-03
    -0.17005393420182E-03
    -0.14292238222802E-03
    -0.12048981961726E-03
    -0.10182978503448E-03
    -0.86178058019242E-04
    -0.72928575326720E-04
    -0.61613399268902E-04
    -0.51877242408502E-04
    -0.43452338487951E-04
    -0.36136444117556E-04
    -0.29774951350710E-04
    -0.24247094860570E-04
    -0.19455757052863E-04
    -0.15320203216670E-04
 0 0 1 0 1
    -0.36741232178207E-01
    -0.22980380651746E-01
    -0.14289660523504E-01
    -0.87248119186359E-02
    -0.52165015737020E-02
    -0.30442559592531E-02
    -0.17187672278215E-02
    -0.92034864768368E-03
    -0.44665388594640E-03
    -0.17180624691644E-03
    -0.18029835246793E-04
     0.62656319960734E-04
     0.99836676728874E-04
     0.11171390876218E-03
     0.10943210935956E-03
     0.99757700760475E-04
     0.86750134905736E-04
     0.72803446694530E-04
     0.59292387637833E-04
     0.46969693677009E-04
     0.36207932127320E-04
     0.27145815868709E-04
     0.19777037041139E-04
     0.14005279090632E-04
     0.96796384761379E-05
     0.66186600707396E-05
     0.46274884107037E-05
     0.35104872762797E-05
     0.30805231563062E-05
     0.31655489297527E-05
     0.36128958849786E-05
     0.42916155098686E-05
     0.50932078733324E-05

输出3.txt

     4.000      0.55977888405323E-01 ! first ligne of f1(x)
     4.250     -0.36741230636835E-01 ! first ligne of f2(X)
     4.500      0.19326476613060E-02
     4.750      0.33961102366447E-01
     5.000      0.33961102366447E-01
     5.250      0.36035027354956E-01
     5.500     -0.49277886748314E-01
     5.750     -0.49277886748314E-01
     6.000     -0.22551828995347E-01
     6.250      0.20274365320802E-01
     6.500      0.20274365320802E-01
     6.750      0.14215507544577E-01
     7.000      0.14215507544577E-01
     7.250      0.28383031021804E-02
     7.500      0.99175411742181E-03
     7.750      0.99175411742181E-03
     8.000     -0.17017297446728E-01
     8.250     -0.17017297446728E-01
     8.500      0.64984848722816E-02
     8.750     -0.79643530771136E-02
     9.000     -0.79643530771136E-02
     9.250      0.10985487140715E-01
     9.500      0.10985487140715E-01
     9.750      0.44212350621819E-02
    10.000      0.44212350621819E-02
    10.250     -0.48470445908606E-02
    10.500      0.47113737091422E-02
    10.750      0.47113737091422E-02
    11.000     -0.30427575111389E-02
    11.250     -0.30427575111389E-02
    11.500     -0.59626842848957E-02
    11.750     -0.59626842848957E-02
    12.000      0.77053293352947E-03
~                                   

标签: fortranfortran90

解决方案


推荐阅读