我想从文本文件中读取我的测量数据。数据有例如以下表格:
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
我做错了什么? :(
提前感谢您的帮助。
答案 0 :(得分: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提到的内容。