Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Read problem in fortran for matrices

  1. Nov 24, 2012 #1
    Have data that is coming from Matlab and want to read it into Fortran. Simple example of what I have:

    Code (Text):

    PROGRAM file_read

    IMPLICIT NONE

    REAL, ALLOCATABLE :: read_matrix(:,:)
    INTEGER :: i,j

    OPEN(1,FILE = 'data_wanted.prn', ACTION = 'READ', STATUS = 'OLD')

    do i = 1,50
    do j = 1,25

    READ(1,*) read_matrix(i,j)

    end do
    end do

    END PROGRAM file_read
     
    I have tried csv, dat, prn file extensions. If I get rid of one of the loops and just read in a vector than I am fine. As soon as I try read in the whole matrix I get the error:

    Fortran runtime error: End of file

    Also, this code worked for me before, but that is when Fortran wrote the data file.

    Any help is appreciated. Thanks.
     
  2. jcsd
  3. Nov 24, 2012 #2

    jedishrfu

    Staff: Mentor

    perhaps try reading a single variable say x and then do an assignment of x to read_matrix:

    read(1,*) x
    read_matrix(i,j)=x

    I'm thinking the read_matrix is telling the compiler to do an internal loop for you to load the whole thing.
     
  4. Nov 24, 2012 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Each READ statement starts reading a new line from the file. If you have more than one number on each line, that is not what you want. (If you get rid of one of the loops, the program probably "runs", but reads in the wrong numbers).

    Try replacing the DO loops with

    READ(1,*) ((read_matrix(i,j),j=1,25),i=1,50)

    That will read all the numbers from the each line of the file, not just the first one.
     
  5. Nov 24, 2012 #4
    AlphZero, thank you. Worked perfectly.
    I would like to ask since it looks like you are in the engineering field and know fortran, how relevant is fortran?

    Just learning it this semester for a class and was wondering.

    Thanks again.
     
  6. Nov 24, 2012 #5
    Also one more question.

    I am using a subroutine to call in all my files. This subroutine is in a module.

    My main program calls the subroutine with empty but allocatted matrices

    Code (Text):

    Program Test

    IMPLICIT NONE

    INTEGER :: num
    REAL, ALLOCATABLE :: test_matrix(:,:)

    Allocate(test_matrix(5,5))

    call file_open(num,test_matrix)


    !This does not print anything, any output is fine now, format later
    write(*,*) test_matrix
    write(*,*) num

    END PROGRAM test

    !*****************************************************

    SUBROUTINE file_open(num,test_matrix)

    IMPLICIT NONE

    INTEGER :: num
    REAL, ALLOCATABLE :: test_matrix(:,:)

    OPEN (1,file='num_file.dat',ACTION = 'READ', STATUS = 'OLD')
    READ(1,*) num

    OPEN (2,file='test_matrix_file.dat',ACTION='READ','STATUS='OLD)
    READ(2,*) ((test_matrix(i,j),i=1,5),j=1,5)

    close(1)
    close(2)

    END SUBROUTINE file_open

     
    Maybe how I am calling the matrices, I am not sure. Thanks for the help.
     
  7. Nov 24, 2012 #6
    Here is your code...fixed.

    Code (Text):

    Program Test

    IMPLICIT NONE

    INTEGER :: num, m, n
    REAL, ALLOCATABLE :: test_matrix(:,:)

    m = 5
    n = 5
    Allocate(test_matrix(m,n))

    call file_open(num,m,n,test_matrix)


    !This does not print anything, any output is fine now, format later
    write(*,*) test_matrix
    write(*,*) num

    END PROGRAM test

    !*****************************************************

    SUBROUTINE file_open(num,m,n,test_matrix)

    IMPLICIT NONE

    INTEGER :: i,j,num, m, n
    REAL, dimension(m,n) :: test_matrix

    !OPEN (1,file='num_file.dat',ACTION = 'READ', STATUS = 'OLD')
    !READ(1,*) num

    OPEN (2,file='test_matrix_file.dat',ACTION='READ',STATUS='OLD')
    READ(2,*) ((test_matrix(i,j),i=1,m),j=1,m)

    !close(1)
    close(2)

    END SUBROUTINE file_open
     
    As far as Fortran being relevant...well, you are going to find that most people don't care for Fortran.

    For what I do (engineering calculations, physics modeling, etc.) I find Fortran as good as any; actually, it is easier to learn and program than C and C++. If you are going to want to create GUI or carry out visualizations, then I would switch to Python...you can easily call fortran from python with f2py.

    As you may be learning, Fortran can handle arrays a-la-matlab, which is pretty handy; typical loops can be handle in a single line, and the code lends itself for optimization for multiple processors, etc.

    For engineering calculations, Fortran is great; but it is not a systems programming language, it is a FORmula TRANslator.
     
  8. Nov 24, 2012 #7

    jedishrfu

    Staff: Mentor

    Most of the engineers I know use MatLab extensively.
     
  9. Nov 25, 2012 #8
    The engineers/physicists I know use matlab to come up with the problem then write it in C/C++/Fortran to parallelize it if is considered computationally needed.
     
  10. Nov 25, 2012 #9
    That modification to the code did not work gsal, but thanks for trying to help.
    When I comment out

    close(2)

    I get the error

    Fortran runtime error: Cannot write to file opened for READ

    referring to the file opened for the array

    if I leave close(2) there, then the main program does not write to the terminal, it just comes up blank.

    If I dont have the read statement in the subroutine then I do get back a 2d array of zeros, which at least there is a baseline.

    Thanks for the help.
     
  11. Nov 25, 2012 #10

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    There's a lot of Fortran code still being used, and it's not economic to convert it all to a different language without adding any value.

    However IMO Fortran is pretty much past its sell by date for NEW applications. The original language design was intended entirely for off-line (batch) computation, and reading and writing files a line at a time rather than a character at a time, simply because that was the only way most computers worked back in 1960. By 1980 there were a set of non-standard extensions to Fortran IV (or Fortran 77, which was pretty much the same language) which every "real Fortran programmer" knew about, and every serious Fortran compiler implemented. Back then, people (including me) were quite happily writing Fortran code using data structures like dynamcially allocated multiply linked lists, and designing programs using OO ideas like classes and inheritance, that ran on a range of hardware and operating systems as different from each other as say IBM mainframes, Cray supercomputers, and assorted proprietary versions of Unix, without any need for the "improvements" in the language to "officially" support what already existed unofficially.

    IMO F90 was basically a political exercise, and the standards committee did an excellent job of redesigning a horse to look like a camel. Adding ever more features to the language in later versions hasn't disguised that fact.

    I don't complain much about maintaining hundreds of thousands of lines of Fortran code as part of my job (if only because most of it is still F77 that hasn't been obfuscated by the "standards police") but Fortran wouldn't be my language of choice for writing any completely new application. And F90/95 wouldn't be my language of choice to write ANY application. Period. No exceptions. Life's too short to waste on typnig nonsense like "REAL, ALLOCATABLE :: test_matrix(:,:)" when you can easily find half a dozen other languages (including "real world but nonstandard" F77!) that express the same idea in half the number of keysstrokes.
     
    Last edited: Nov 25, 2012
  12. Nov 25, 2012 #11
    I figured it out. I was using Open(6,blahblah), and then I vaguely remembered some compilers dedicate the first few numbers for something (correct me if I am wrong). Anyways, changed 6 to 50 and now works.
    Thank for the help.

    Fortran does seem antiquated, but it seems like for heavy duty calculations it is a must, especially for using Open_MPI, Open_MP, and CUDA. Does this seem accurate?

    Thanks again.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Read problem in fortran for matrices
  1. FORTRAN READ problems (Replies: 5)

Loading...