0

我想从文本文件中读取我的测量数据。数据具有例如以下形式:

0               0.0531139       
0.000157095     0.306123        
0.000314191     0.133868        
0.000471286     0.29799     
0.000628381     0.0182098       
0.000785477     0.121222        
0.000942572     0.32111     
0.00109967      0.0267326       
0.00125676      0.49554     
0.00141386      0.433729    

我的代码如下:

 SUBROUTINE test(name)
 implicit none
  character                             :: name*(*)
  real*8,allocatable, dimension(:,:)    :: measMatrix
  integer                               :: i,
 &                                         nrcols
  nrcols = 2

  nrrows = 10

  allocate(measMatrix(nrcols,nrrows))

  open(unit = 20, file = Dateiname, status = 'old', action = 'read')

  do i = 1, nrrows
      read(20,*) measMatrix(i,1:nrcols)
  end do

  close(20)

  open(unit = 10, file = 'Test4.txt')      
  do i = 1,nrrows
      write(10,777) (measMatrix(i,j), j = 1,nrcols)
  end do
  close(10)
777   format(F12.9,4X,F12.9)
  deallocate(measMatrix)
      END

但是,输出是错误的:

 0.000000000     0.000314191
 0.000157095     0.000471286
 0.000314191     0.000628381
 0.000471286     0.000785477
 0.000628381     0.000942572
 0.000785477     0.001099670
 0.000942572     0.001256760
 0.001099670     0.001413860
 0.001256760     0.495540000
 0.001413860     0.433729000

我究竟做错了什么 ?:(

在此先感谢您的帮助。

4

1 回答 1

0

第一个维度是禁食变化的维度,也是内存中连续的维度。

因此,在内存空间中,您的 (10,2) 布局为:

 1 11
 2 12
 3 13
 4 14
 5 15
 6 16
 7 17
 8 18
 9 19
10 20

也许你想要这个:

nrrows = 10
nrcols = 2
allocate(measMatrix(10,2))

do i = 1, nrrows
  read(20,*) measMatrix(i,:)
end do
...   
do i = 1, nrrows
  write(10,777) measMatrix(i,:)
end do

我更喜欢这个:

integer          :: Crnt_Row, Crnt_Col

nrrows = 10
nrcols = 2
allocate(measMatrix(10,2))
WRITE(*,*)'Shape(measMatrix)=',SHAPE(measMatrix)

do CurrRow = 1, nrrows
  read(20,*) measMatrix(CurrRow,:)
end do
...   
do CurrRow = 1, nrrows
  write(10,777) measMatrix(CurrRow,:)
end do

使用 IMPLICIT NONE 也将有助于遵循@d_1999 提到的内容。

于 2016-11-09T13:21:57.143 回答