从Matlab生成的csv文件读取数据时遇到一个小问题。
首先提供一些一般信息:
OS:MAC OS Catalina 10.15.3Fortran编译器:gfortran 9.2(据报道是2019年10月更新,适用于Catalina OS)。编程环境:Elcipse 2019-12(4.14.0)
这是我遇到问题的代码段:
filename='positie_frame_cam'
extension='.txt'
do icam=1,6,1
write (getal,'(I1)') icam ! converting integer to string using an 'internal file'
totalname=filename//getal//extension
print*,totalname
open(unit=icam+10,file=totalname, form='formatted',action='read')
nlines(icam)=0
iop=0
i=0
do while (iop.ge.0)
i=i+1
write(0,*)' regel nummer ',i
read(icam+10,*,iostat=iop)tel,DUMY(1),DUMY(2),&
DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
write(0,*) 'iostat=',iop
write(0,*) tel,DUMY(1),DUMY(2),&
DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
nlines(icam)=nlines(icam)+1
if (iop.ge.0) then
nlines(icam)=nlines(icam)-1
write(0,*) 'something went wrong!'
close(icam+10)
endif
if (iop.eq.-1)then
write(0,*)'eof'
close(icam+10)
exit
endif
end do
end do
以下是我尝试读取的6个文件的示例:
1,0.00000000,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.00833892,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.01667784,0.36486442,0.05303874,0.59982310,-0.02163688,0.99431165,-0.10428895
1,0.02501677,0.35989577,0.05282108,0.59855295,-0.02244646,0.99420581,-0.10512358
1,0.03335569,0.35492529,0.05268669,0.59727332,-0.02325602,0.99409861,-0.10595804
1,0.04169461,0.35368452,0.05266613,0.59726437,-0.02341294,0.99407797,-0.10611699
1,0.05003353,0.35491671,0.05268653,0.59602339,-0.02343777,0.99407317,-0.10615646
1,0.05837246,0.35862988,0.05277895,0.59479665,-0.02314764,0.99410979,-0.10587683
1,0.06671138,0.36359068,0.05297500,0.59481924,-0.02251773,0.99419203,-0.10523858
1,0.07505030,0.36483061,0.05303698,0.59482341,-0.02236010,0.99421248,-0.10507885
这是我对一个文件的输出:
Last login: Thu Mar 12 09:19:00 on ttys001
/Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) wlan-145-94-195-39:~ fclemens$ /Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;
positie_frame_cam1.txt
regel nummer 1
iostat= 0
1 0.0000000000000000 0.36611264999999998 5.3106540000000001E-002 0.60107655999999998 -2.1298850000000001E-002 0.99435631000000002 -0.10393212000000000
something went wrong!
regel nummer 2
iostat= -1
1 0.0000000000000000 0.36611264999999998 5.3106540000000001E-002 0.60107655999999998 -2.1298850000000001E-002 0.99435631000000002 -0.10393212000000000
eof
因此,显然已成功读取了记录1,但是随后再次读取了第一个记录而不是第二个记录,并检测到eof。我不确定如何解决此问题。
您的代码清楚地在输出样本中写入了something went wrong!
。
write(0,*) 'something went wrong!'
close(icam+10)
这意味着文件已关闭,然后它尝试再次从关闭的单元读取数据,并且运行不正常。
[最有可能,您想使用.gt.
代替]中的[C0
.ge.
但是,我强烈!建议改用if (iop.ge.0) then
。它更具可读性,您更有可能发现问题,而不必在互联网上询问。
此后,如果在此if分支的>
语句后需要exit
。