Reading matrix elements from a file in Fortran77

In summary, the individual is asking for help in reading a matrix written in a txt file and using it in a Fortran77 program. They are unsure of how to use the read statement to get the elements from the file into the array. After discussing possible solutions, the individual discovers that using "read(file,*)A(i,j)" is the solution.
  • #1
Telemachus
835
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
  • #2
That's great, thanks for sharing your solution too.
 
  • Like
Likes Telemachus

1. How do I read matrix elements from a file in Fortran77?

In order to read matrix elements from a file in Fortran77, you can use the READ statement followed by the name of the file and the appropriate format specifier. For example, if your file contains a 3x3 matrix of integers, you could use the statement READ(1,*) A(3,3) to read the entire matrix into the array A.

2. What format specifier should I use to read matrix elements from a file in Fortran77?

The format specifier you use will depend on the type of data stored in the file. For integers, you can use I, for real numbers you can use F, and for characters you can use A. You can also use a combination of these specifiers to read in different types of data. It is important to make sure that the format specifier matches the data in the file, otherwise you may encounter errors.

3. Can I read in a matrix of any size from a file in Fortran77?

Yes, you can read in a matrix of any size from a file in Fortran77. However, you will need to make sure that the array you are reading the elements into is large enough to hold all of the data. If the array is not large enough, you may encounter errors or the data may not be read correctly.

4. How can I handle errors when reading matrix elements from a file in Fortran77?

You can use the ERR specifier in your READ statement to handle errors when reading matrix elements from a file in Fortran77. This will allow you to specify what should happen if an error occurs, such as displaying an error message or stopping the program.

5. What is the best way to organize my code when reading matrix elements from a file in Fortran77?

One way to organize your code is to create a separate subroutine specifically for reading in the matrix elements from the file. This will make your code more modular and easier to read. You can also use comments and proper indentation to make your code more organized and understandable.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
21
Views
510
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
1
Views
347
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
4
Views
739
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
974
Back
Top