Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

save the array to a txt file

IDZ_A_Intel
Employee
3,573 Views

Hi,

If I want to save a big 2-D matrix (2000*441), is there any existing subroutine from IVF or MKL that I can use? I just want to save it to a txt file.

Thanks,

Ying

0 Kudos
6 Replies
IDZ_A_Intel
Employee
3,573 Views

Ying,

Do you intend to read this file with your eyes, or read it back later with a program?

If with a program, is this with your Fortran program or a spreadsheet.

If a program is aspreadsheet then do you want comma delimited or fixed field formatting?

If with your eyes, what format or appearence do you want?

Please elaborate as to how you want to use this txt file.

Jim Dempsey

0 Kudos
DavidWhite
Valued Contributor II
3,573 Views

I would add that if you want to read it into Excel, then tab-delimited text is probably better than comma delimited. Tab is CHAR(9).

David

0 Kudos
IDZ_A_Intel
Employee
3,573 Views

Hi Jim and David,

Thanks for your kind answers. I want to write the big real matrix into a plat txt file and read it by matlab or excel. Then visulize the matrix by graphics. I think a tab-delimited format may be better for me.

Again, the reason I do this is to visualize the results by using matlab, however the fortran until now does not have a library like matlab for scientific visualization. So I have to read the data in matlab firstly (write the matrix first firstly:-). I know Intel has a library, named intel array visualizer, which is good enough. But it only supports Windows. I use Linux some time.

By the way, could I ask is it true that intel array visualizer is not supported by Intel any more? But for me, this is a very smart and handy library.

Thanks very much.

Ying

0 Kudos
Steven_L_Intel1
Employee
3,573 Views
It is true that Array Visualizer is not supported, but it is available as a free download.
0 Kudos
IDZ_A_Intel
Employee
3,573 Views

Ying,

Something like this might work out for you:

[bash]program BigFile
  implicit none
  interface
    subroutine Write2DArrayToFile(U, A, FMT)
      implicit none
      integer :: U
      real(8) :: A(:,:)
      CHARACTER(*) :: FMT
    end subroutine Write2DArrayToFile
  end interface
  real(8) :: YourArray(12,34)
  integer :: row, col
  do row = LBOUND(YourArray, 1), UBOUND(YourArray, 1)
    do col = LBOUND(YourArray, 2), UBOUND(YourArray, 2)
      YourArray(row,col) = row * col
    end do
  end do
  open(1,NAME='data.txt',STATUS='REPLACE',ACCESS='STREAM',CARRIAGECONTROL='NONE')
  call Write2DArrayToFile(1,YourArray,'(F10.2)')
  close(1)
end program BigFile

subroutine Write2DArrayToFile(U, A, FMT)
  implicit none
  integer :: U
  real(8) :: A(:,:)
  CHARACTER(*) :: FMT
  integer :: row, col
  CHARACTER :: TAB
  CHARACTER(2) :: CRLF 
  CHARACTER(80) :: FMTBUFF
  TAB = CHAR(8)
  CRLF(1:1) = CHAR(13)
  CRLF(2:2) = CHAR(10)
  do row = LBOUND(A, 1), UBOUND(A, 1)
    do col = LBOUND(A, 2), UBOUND(A, 2)
      write(FMT=FMT,UNIT=FMTBUFF) A(row, col)
      FMTBUFF = adjustl(FMTBUFF)
      WRITE(UNIT=U) FMTBUFF(1:LEN_TRIM(FMTBUFF))
      IF(col .lt. UBOUND(A, 2)) WRITE(UNIT=U) TAB
    end do
    write(UNIT=U) CRLF
  end do
end subroutine Write2DArrayToFile
[/bash]

Jim Dempsey

0 Kudos
peterklaver
Beginner
3,573 Views

A less elegant way of getting large arrays from fortran into Matlab that I've used before it to simply dump the array from the fortran code into a column-major arrangement like this:

[fortran]open (unit=1,file='YourArrayDump.txt',status='replace')
do j=1,ncols
  do i=1,nrows
    write (1,*) YourArray(i,j)
  end do
end do[/fortran]
Then in Matlab, read and reshape thusly:

[python][YourArrayTemp]=textread('YourArrayDump.txt','%f');
YourArray=reshape(YourArray,nrows,ncols);[/python]

I have used this for arrays at least as large as what you mention in your first post. It's crude, but it's effective. For Excel you would have to go the tab-delimited route.
0 Kudos
Reply