Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read matrix file

0 views
Skip to first unread message

Eugene

unread,
Jan 4, 2009, 8:28:00 AM1/4/09
to
Hi there,
I am thinking a way to load file, containing matrix data, by Fortran
code. As you might know that a simple command load() in MatLab can do
a good job to read data (N by M matrix) to array M(n,m). So I am
trying to write a subroutine by Fortran Code. But got some difficult.
Anyone can help with this?

module class_datain
integer(4),parameter::FileUnit=9999
interface load
module procedure loadMatrix
end interface load
contains
subroutine loadMatrix(FileName,matrix,n,m)
implicit none

character(*),intent(in)::FileName !file containing matrix data
integer(4),intent(in),optional::n,m !dimension of matrix
integer(4):: i,j,NumRow,NumCol,ios
real(8),intent(out),allocatable::matrix(:,:)
real(8)::x


open(FileUnit,file="FileName") !Inquire if the file exists or
not before this command

! if n and m present
if(present(n).and.presnet(m)) then
NumRow=n
NumCol=m
else
!determine n and m from file
NumCol=0
NumRow=0
do while(.true.)
read(FileUnit,*,advance='no',status=ios) x

if(ios .lt. 0) then !end of record reached
rewind
exit
else
NumCol=NumCol+1
end if
end do
do while(.true.)
read(FileUnit,*,end=110) x
NumRow=NumRow+1
end do
110 continue
end if
close(FileUnit)
!check n and m
if(n.ge.1.and.m.ge.1) then
if(allocated(matrix)) deallocate(matrix)
allocate(matrix(n,m))
else
write(*,*) "load error: no data found in the file"
end if

!read data
open(FileUnit,file="FileName")
do i=1,NumRow
do j=1,NumCol
read(FileUnit,*,advance='no',iostat=ios) matrix(i,j)

end do
end do

end subroutine loadMatrix
end module

Arjen Markus

unread,
Jan 4, 2009, 9:06:05 AM1/4/09
to
On 4 jan, 14:28, Eugene <yjyi...@gmail.com> wrote:
> Hi there,
> I am thinking a way to load file, containing matrix data, by Fortrancode. As you might know that a simple command load() in MatLab can do
> a goodjobto read data (N by M matrix) to array M(n,m). So I am

> trying to write a subroutine by Fortran Code. But got some difficult.
> Anyone can help with this?
>
> moduleclass_datain
>         integer(4),parameter::FileUnit=9999
>     interface load
>         moduleprocedure loadMatrix

You should at the very least tell us what difficulties you run into.
A few things I can see from this code:
- open(FileUnit, file = "FileName") is going to open a file by the
name "FileName",
not a file whose name is stored in the variable FileName, as you
seem to intend.
Drop the quotation marks (")
- the code you showed us won't compile, as there is a typo - presnet()
in stead of present().
- list-directed input can not be combined with advance='no'.

What you can try, is something along these lines:

character(len=1000) :: line
real :: dummy
integer :: i, ierr, k

read(FileUnit,'(a)') line
do i = 1,len(line)
read(line, *, iostat = ierr) (dummy, k=1,i)
if ( ierr /= 0 ) then
num_col = i - 1
exit
endif
enddo

The trick is: read the entire first line in a character string and
then try
to read more and more numbers from it.

(If you expect lines of arbitrary length, then there are solutions for
that
as well, but slightly too complicated for this sunday afternoon :)
Hint: the
length of a character string can be dynamic in a subroutine/function)

Regards,

Arjen

Eugene

unread,
Jan 4, 2009, 4:42:13 PM1/4/09
to

Great point.
Several things I did not clarify.
1, That module I posted is just want to show my idea. I did not
compile it at all. So typo and error are not surprise.
2,FileName is the dummy variable which is the file name passed by the
actual variable. So you are right, the quotation marks should not be
there.
3,My difficult is when I tried to write a read statement like:
read(FileUnit,fmt=*,advance='no') x
Compiler told me the 'advance' parameter can not used with 'fmt=*'. I
was wondering that is because I did not use those parameters properly.
Thank you for sharing the trick. It sounds nice.

Arjen Markus

unread,
Jan 5, 2009, 2:35:12 AM1/5/09
to
> Thank you for sharing the trick. It sounds nice.- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

Another way of determining the number of values on a line of unknown
length is:
- Read the characters one by one
- Count the number of transitions from space to non-space (where you
assume
that the "previous" character for the very first one is a space)

This may actually be simpler to implement than what I suggested
before.
(Taking care of all the properties of list-directed input, like ,,
skipping
a value and 2*1.0 being two 1.0's in a row, will complicate the
procedure,
but my guess is you do not want them in the first place.)

Regards,

Arjen

0 new messages