# I'm getting garbage values while reading matrices from a file

#### kranthi4689

Hi All,
While trying to read a matrix from data file using fortran90 code ,I get garbage values and a backtrace error.
 Error termination. Backtrace: #0 0x7f4a4de3631a #1 0x7f4a4de36ec5 #2 0x7f4a4de3768d #3 0x7f4a4dfa4d42 #4 0x7f4a4dfa6ad5 #5 0x7f4a4dfa80f9 #6 0x56040bbeae57 #7 0x56040bbeaf93 #8 0x7f4a4da4bb96 #9 0x56040bbeaa29 #10 0xffffffffffffffff 

The code I use is
program read_file
implicit none
integer :: ios,i,j
character (len =40) :: str_name
character (len=1000) :: line
integer , dimension (10) :: vect
real ,  dimension (11,16) :: hamilt
str_name = ' OVERLAP MATRIX - CELL N.   1(  0  0  0)'
open(unit=20,FILE='data.txt',iostat=ios,status='old')
if ( ios /= 0 ) stop "Error opening inputfile"
do
if (line(1:40) == str_name ) then
write (*,*) "found line"
write (*,*) line
!read (20,*) (a(i,j), j = 1,16))
do i = 0,10
write (*,*) hamilt(i,j)
enddo
endif
enddo
100 close(20)
end program
I attached my data file. I want to read the matrix that starts after str_name.
Kindly help me in understanding the error in my code. thank you.

#### Attachments

• 15.1 KB Views: 29
Related Programming and Computer Science News on Phys.org

#### WWGD

Gold Member
Are you reading this files into some other software, say, Excel, or is this self-contained?

#### kranthi4689

Are you reading this files into some other software, say, Excel, or is this self-contained?
@WWGD I just want to read them and write them in a seperate txt file.

#### DrClaude

Mentor
Fortran:
          do i = 0,10
write (*,*) hamilt(i,j)
enddo
1st error: i starts at zero, but Fortran array indices start at 1.
2nd error: the structure in the file doesn't follow what you are doing here. The outer loop should go from i = 1 to 16, then the inner loop should go from 1 to min(i,10). You then have to add code to read the rest of the matrix (since only the first 10 columns are found in the first block).

#### DrClaude

Mentor
Forgot the third error: you are neglecting to read the index that appears to the left of the matrix elements.

#### kranthi4689

1st error: i starts at zero, but Fortran array indices start at 1.
2nd error: the structure in the file doesn't follow what you are doing here. The outer loop should go from i = 1 to 16, then the inner loop should go from 1 to min(i,10). You then have to add code to read the rest of the matrix (since only the first 10 columns are found in the first block).
I rectified the code to address the errors, but I still get segmentation faults.
Code:
 read (20,*) (vecy(i), j = 1,10) ! read column index
do i = 1,16
do j = 1,min(i,10)
read(20,*,end=100) vecx(i) , hamilt(i,j) ! row index and matrix elements
write (*,*) hamilt(i,j)
enddo
enddo
endif

#### DrClaude

Mentor
You can't read vecx inside the j loop. You have to use an implicit loop as before.

#### kranthi4689

You can't read vecx inside the j loop. You have to use an implicit loop as before.
reading it out the loop is giving a fortran runtime error.

#### DrClaude

Mentor
Fortran:
read (20,*,end =100) vecx(i), (hamilt(i,j), j=1,min(i,10))

#### kranthi4689

Fortran:
read (20,*,end =100) vecx(i), (hamilt(i,j), j=1,min(i,10))
It worked Thank you. Could you kindly recommend a book or site or some material from where I can learn fortran efficiently. Thank you.

#### WWGD

Gold Member
Wow, I thought Fortran was a dead language outside of dealing with legacy systems.

#### DrClaude

Mentor
It worked Thank you. Could you kindly recommend a book or site or some material from where I can learn fortran efficiently. Thank you.

But if I may, I would like to point out that the problem you were having has nothing to do with Fortran per se. I have a feeling you would have stumbled whichever language was used. It is important to decompose what needs to be done step by step and then program these steps.

Wow, I thought Fortran was a dead language outside of dealing with legacy systems.
Far from it. Especially with the newer versions of Fortran, the language has now most of the features of modern languages. I would say that for numerical computations, it is still one of the best options available.

#### Dr Transport

Gold Member
Wow, I thought Fortran was a dead language outside of dealing with legacy systems.
No, not dead at all, my lab uses it exclusively for large scale computational efforts. I'd convert to C++ but I'm the youngest member of my group and the other 4 members don't know a line of C/C++, so we are continuing with Fortran for the time being, when they start to retire, we'll convert over.

"I'm getting garbage values while reading matrices from a file"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving