Reading matrix elements from a file in Fortran77

Click For Summary
SUMMARY

This discussion addresses the process of reading matrix elements from a text file in Fortran77. The user successfully implemented a solution using the Fortran77 read statement to populate a two-dimensional array from a file containing matrix elements in row-major order. The specific code used was read(file,*) A(i,j), which effectively reads the data into the array A(rows, cols). This solution resolves the user's initial uncertainty regarding the correct syntax for reading matrix data.

PREREQUISITES
  • Understanding of Fortran77 syntax and programming structure
  • Familiarity with file handling in Fortran77
  • Knowledge of matrix representation in programming
  • Basic concepts of row-major order data storage
NEXT STEPS
  • Explore advanced file I/O techniques in Fortran77
  • Learn about dynamic memory allocation in Fortran77
  • Investigate error handling in Fortran77 file operations
  • Study optimization techniques for matrix operations in Fortran
USEFUL FOR

This discussion is beneficial for Fortran77 programmers, particularly those working with matrix data structures and file input/output operations. It is also useful for students learning numerical methods and data manipulation in Fortran.

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   Reactions: Telemachus

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
7K
  • · Replies 6 ·
Replies
6
Views
2K