【问题标题】:Get from fortran into an excel file fortran (ftn95)从 fortran 获取到 excel 文件 fortran (ftn95)
【发布时间】:2013-10-15 10:41:45
【问题描述】:

我正在使用 fortran f95。我的操作系统是 Windows 7,64 位。

我想将输出保存到 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

**

【问题讨论】:

  • 你知道如何使用 Fortran write 语句吗?如果你这样做了,用它来写一个csv文件; Excel 将轻松读取此类文件。如果您不知道 write 语句,请进行一些学习,并在您有需要帮助的代码时回来。
  • 除了 HPM 所说的之外,还有几种方法可以解决这个问题;一种是前面提到的 csv 方法。效果相当好。最小的麻烦。如果您正在考虑将不断重新加载和一次又一次地绘制的东西,那么可能值得研究一个宏,该宏将读取输出(无论它可能是什么)文件并将其读入 Excel 工作表。还有一些用于“直接”编写 excel 文件的库,但那将是最复杂的方法。

标签: excel fortran fortran95


【解决方案1】:

将数据输出到文件中,以空格或逗号分隔。 Excel 具有文本导入功能,可以正确处理和显示此类文件。

一个示例是 ASCII tecplot 文件,您可以使用 Fortran 简单地编写它,并提供源代码和示例here - TECPLOT_WRITE。接下来是一个简短的 sn-p。

!
!  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2014-06-26
    • 2015-07-09
    • 2021-03-04
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    相关资源
    最近更新 更多