2013-10-15 58 views
3

我正在使用fortran f95。我的操作系統是Windows 7,64位。從fortran轉換成excel文件fortran(ftn95)

我想獲得輸出到excel文件,以便我可以繪製數據。有人知道怎麼做這個嗎?非常感謝您的回覆。 PS:我希望輸出文件包含x,f(i),fprime1,fprime2,fprime3,diff1,diff2和diff3。該代碼是如下所述:

**

! This program calculates the first derivative of 
! a function, where f(x)= sin x. It makes use of the 
! centred-difference formula using 3 values of the step size: h1, h2, h3. 
! It also calculates the 
! analytical first derivative of the function. 
Program centred_difference_first_derivative 
implicit none 
real :: x, h1, h2, h3, fprime1, fprime2, fprime3, diff1, diff2, diff3, pi, stepa 
real,dimension(:), allocatable :: f 
integer :: i 
! Assignment of variables 
x=0.0 
pi=4*atan2(1.0,1.0) 
allocate(f(41)) 
stepa=pi/20.0 
h1=0.1 
h2=0.01 
h3=0.001 
! Calculate analytical derivative of sin x 
! for the domain x:[0,2pi] 
do i=1,41 
    f(i)=cos(x) 
    x=x+stepa 
    end do 

! Approximates first derivative of sin x 
! step size h1, for the domain x:[0,2pi] 
    x=0.0 
do i=1,41 
    fprime1=(sin(x+h1)-sin(x-h1))/(2*h1) 
    diff1=f(i)-fprime1 
    print 37, x,f(i),fprime1,diff1 
    x=x+stepa 
    end do 
37 format(e15.8,3x,e15.8,3x,e15.8,3x,'ERROR1= ',e15.8) 

! Approximates first derivative of sin x 
! step size h2, for the domain x:[0,2pi] 
    x=0.0 
do i=1,41 
    fprime2=(sin(x+h2)-sin(x-h2))/(2*h2) 
    diff2=f(i)-fprime2 
    print 49,x,f(i),fprime2,diff2  
    x=x+stepa 
    end do 
49 format(e15.8,3x,e15.8,3x,e15.8,3x,'ERROR2= ',e15.8) 
! Approximates first derivative of sin x 
! step size h3, for the domain x:[0,2pi] 
    x=0.0 
do i=1,41 
    fprime3=(sin(x+h3)-sin(x-h3))/(2*h3) 
    diff3=f(i)-fprime3 
print 61,x,f(i),fprime3,diff3  
    x=x+stepa 
    end do 
61 format(e15.8,3x,e15.8,3x,e15.8,3x,'ERROR3= ',e15.8) 
end program 

**

+7

您是否知道如何使用Fortran'write'語句?如果你這樣做,用它來寫一個'csv'文件; Excel將輕鬆讀取這樣的文件。如果你不知道'write'語句做了一些學習,並且當你有你想要的幫助時回來。 –

+1

除了HPM所說的之外,還有幾種方法可以解決這個問題;一個是前面提到的csv方法。工作得很好。最小的麻煩。如果你正在考慮一些不斷重新加載和繪製的東西,可能需要研究一個宏,它將讀取輸出(不管它可能是什麼)文件並將其讀入Excel工作表。也有「直接」編寫excel文件的庫,但這將是最複雜的方法。 – Rook

回答

1

輸出數據到文件時,由空格或逗號分隔。 Excel具有文本導入功能來正確處理和顯示這些文件。

一個示例是ASCII tecplot文件,您可以使用Fortran簡單編寫,其源代碼和示例提供了here - TECPLOT_WRITE。接下來是一小段代碼。

! 
! Write the zone header. 
! 
    write (iunit, '(a)') ' ' 
    write (iunit, '(a,i6,a,i6,a,i6,a)') 'Zone I=', nr, ', J=', nz, 'K=', & 
    nt, ', F=POINT' 
! 
! Write the zone data, one node at a time. 
! 
    do k = 1, nt 
    do j = 1, nz 
     do i = 1, nr 

     x = r(i) * cos (t(k)) 
     y = r(i) * sin (t(k)) 

     vx = vr(i,j) * cos (t(k)) - vt(i,j) * sin (t(k)) 
     vy = vr(i,j) * sin (t(k)) + vt(i,j) * cos (t(k)) 

     write (iunit, '(3f10.3,3g15.6)') x, y, z(j), vx, vy, vz(i,j) 

     end do 
    end do 
    end do