Register to reply

Read problem in fortran for matrices

by autobot.d
Tags: fortran, matrices
Share this thread:
autobot.d
#1
Nov24-12, 11:15 AM
P: 68
Have data that is coming from Matlab and want to read it into Fortran. Simple example of what I have:

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.
Phys.Org News Partner Science news on Phys.org
NASA team lays plans to observe new worlds
IHEP in China has ambitions for Higgs factory
Spinach could lead to alternative energy more powerful than Popeye
jedishrfu
#2
Nov24-12, 11:28 AM
P: 2,773
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.
AlephZero
#3
Nov24-12, 12:02 PM
Engineering
Sci Advisor
HW Helper
Thanks
P: 6,924
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.

autobot.d
#4
Nov24-12, 04:32 PM
P: 68
Read problem in fortran for matrices

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.
autobot.d
#5
Nov24-12, 05:31 PM
P: 68
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

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.
gsal
#6
Nov24-12, 07:02 PM
P: 872
Here is your code...fixed.

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.
jedishrfu
#7
Nov24-12, 09:10 PM
P: 2,773
Most of the engineers I know use MatLab extensively.
autobot.d
#8
Nov25-12, 11:11 AM
P: 68
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.
autobot.d
#9
Nov25-12, 12:45 PM
P: 68
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.
AlephZero
#10
Nov25-12, 01:39 PM
Engineering
Sci Advisor
HW Helper
Thanks
P: 6,924
Quote Quote by autobot.d View Post
how relevant is fortran?
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.
autobot.d
#11
Nov25-12, 02:40 PM
P: 68
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.


Register to reply

Related Discussions
FORTRAN 77 read statement Engineering, Comp Sci, & Technology Homework 4
Fortran read Programming & Computer Science 9
Fortran 90: Read in data Programming & Computer Science 0
Fortran read in flags? Programming & Computer Science 3