Read problem in fortran for matrices

In summary: SunOS on DEC VAXes. But by 1990, these days generally considered the "golden age" of computing, Fortran had largely been superseded by other languages in mainstream use, and the market for Fortran compilers was shrinking. There are still some niche applications which use Fortran, but they are declining in numbers.
  • #1
autobot.d
68
0
Have data that is coming from Matlab and want to read it into Fortran. Simple example of what I have:

Code:
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.
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #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.
 
  • #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:
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.
 
  • #6
Here is your code...fixed.

Code:
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
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.
 
  • #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 don't 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.
 
  • #10
autobot.d said:
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.
 
Last edited:
  • #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.
 

What is Fortran?

Fortran is a high-level programming language commonly used for scientific and numeric computing. It was developed in the 1950s and has been updated over the years to support modern computing architectures.

What is a matrix in Fortran?

In Fortran, a matrix is a two-dimensional array of elements that can be used to store and manipulate data. It is especially useful for performing mathematical operations on large sets of data.

How do I read a matrix in Fortran?

To read a matrix in Fortran, you can use the READ statement, which allows you to specify the format of the data and the location where it should be stored. You can also use the OPEN statement to open a data file and read the matrix from there.

What is a problem statement in Fortran?

A problem statement in Fortran is a description of the specific task or problem that the code is intended to solve. This includes the inputs, outputs, and any relevant equations or algorithms that will be used to solve the problem.

How can I efficiently read large matrices in Fortran?

To read large matrices efficiently in Fortran, you can use the DIRECT access mode when opening the data file. This allows for faster access to specific parts of the matrix. You can also use the BLOCKSIZE option to specify the size of the blocks to be read, which can improve performance for certain types of data.

Similar threads

  • Programming and Computer Science
Replies
4
Views
573
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top