Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: read numerical text file by fixed column width, no delimiters
Replies: 11   Last Post: Jun 20, 2013 4:50 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Derek Goring

Posts: 3,919
Registered: 12/7/04
Re: read numerical text file by fixed column width, no delimiters
Posted: Jun 19, 2013 5:44 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Thursday, June 20, 2013 8:06:42 AM UTC+12, dpb wrote:
> On 6/19/2013 2:36 PM, Kristin wrote:
>
> ...
>
>
>
> OK, here's a very rough, not terribly user=friendly outline, but should
>
> get the job done...
>
>
>
> program tentoeleven
>
> character(len=132):: fold, fnew ! A filename for old, new file
>
> real :: v(8) ! Data array of 8 floats
>
>
>
> read(*,*) fold ! It'll wait for you...
>
> open(11,file=fold,action='read',status='old')
>
>
>
> read(*,*) fnew ! It'll wait for you...
>
> open(12,file=fnew,action='write',status='new')
>
>
>
> ios=0
>
> do while(ios.eq.0)
>
> read(11,'(F10.4)',iostat=ios) v ! read the old file F10.4
>
> write(12,'(F11.4)') v ! F11.4 will insert space delimiter
>
> end do
>
>
>
> close(11)
>
> close(12)
>
> end
>
>
>
> Will continue 'til EOF condition on the old file at which time the
>
> IOSTATUS variable will become <0. Of course, other errors will also
>
> abort, but one presumes the files are otherwise, clean.
>
>
>
> --


Here's an alternative that can be run within Matlab using:
dos(['tentoeleven ' infile ' ' outfile])
where infile and outfile are strings with the filenames.

program tentoeleven
character(len=132):: fold, fnew ! A filename for old, new file
real :: v(8) ! Data array of 8 floats
integer :: na ! No of args on command line
integer :: ios ! Error code for read

na=iargc()
if(na < 2)stop 'Must specify infile and outfile on command line'
call getarg(1,fold)
call getarg(2,fnew)

open(11,file=fold,action='read',status='old')
open(12,file=fnew,action='write',status='new')

ios=0
do while(ios.eq.0)
read(11,'(F10.4)',iostat=ios) v ! read the old file F10.4
write(12,'(F11.4)') v ! F11.4 will insert space delimiter
end do

close(11)
close(12)
end



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.