Fortran Reading matrix elements from a file in Fortran77

Click For Summary
The discussion revolves around reading matrix elements from a text file in row-major order for use in a Fortran77 program. The user initially expressed uncertainty about using the read statement to populate a matrix array from a file. The matrix elements are formatted in a specific way, with each element on a new line. The solution was found to be straightforward: using the read statement in the format `read(file,*) A(i,j)` within nested loops to correctly fill the matrix array A with the data from the file 'matrix.dat'. This resolved the user's issue effectively.
Telemachus
Messages
820
Reaction score
30
SOLVED

Hi there. I have the elements of a matrix written in a txt file (in row major order). I need to read this matrix to use it in my fortran77 program. The text file contains the elements written in this way:

A(1,1)
A(1,2)
...
A(1,N)
...
A(N,N-1)
A(N,N).

I was thinking in doing a do loop, but I haven't used the read statement before for this kind of tasks. So I wasn't sure on how to do this.
Fortran:
        open (unit=10,file='matrix.dat',status='old')

       do i=1,cols
         do j=1,rows
           A(i,j)=?
         enddo
      enddo

I don't know what the statement at ? should be, in order to get the elements of the file matrix.dat written in the array A(rows,cols).

Thanks in advance.

Edit: just had to use read(file,*)A(i,j)!
 
Last edited:
Technology news on Phys.org
That's great, thanks for sharing your solution too.
 
  • Like
Likes Telemachus
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
35
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K