Re: Formatted read of text file [message #9289 is a reply to message #9154] |
Thu, 12 June 1997 00:00   |
Andy Bristow
Messages: 6 Registered: June 1997
|
Junior Member |
|
|
Matt Cheselka wrote:
>
> I have a file that contains row and column data like the following:
>
> # I E E_conv E_bl E_saa Resid_RMS
> # 0 3.512799e-05 3.512799e-05 0.000000e+00 0.000000e+00 3.512799e-05
> 1 1.445887e-05 1.445921e-05 0.000000e+00 0.000000e+00 1.445921e-05
> 2 7.363944e-06 7.364333e-06 0.000000e+00 0.000000e+00 7.364333e-06
> .
> .
> .
>
> So my question is a two-parter.
>
> First, how to I skip the first two lines of this file?
>
> Second, what is proper way to do a formatted read of each of the 6 columns in
> the remaining rows? I would like to have each column value put into its
> own array so I can then plot/print out each column individually.
>
> My guess is that I have to use and OPENR first to open the file, but that
> may not even be the best way to do it.
>
Okay. You hit the nail with the OPENR guess. You don't really need a
FORMAT statement or anything - the data will read in fine without one as
long as there is at least 1 space between each column. Something like
this should work.
;+++
rubbish=''
; open the file
openr,1,filename
; read the 2 first lines to get to the data
readf,1,rubbish
readf,1,rubbish
; num is number of rows of data
; set up a data array for floating numbers - don't worry about the
integer in the
; 1st column, it will work
data=fltarr(6,num)
; read the data
readf,1,data
;close the file
close,1
;+++
You would then have a 6 x num array. You could split this up into 6
individual arrays with:
array1=reform(data(0,*))
array2=reform(data(1,*))
etc
The first column is a integer, so if you want it in integer form just do
array1=fix(array1)
Hope this helps
Andy
P.S. If you don't know how may rows are in the file (for the num
variable, above), then start the same, declare a temporary array
(temp=fltarr(6)) and a main data array big enough to hold the maximum
amount of data yuo will read in (data(fltarr(6,maxnum)). Then
count=0
while NOT(EOF(1)) do begin
readf,1,temp
data(*,count)=temp(*)
count=count+1
endwhile
Then reform the data according to how many records there actually were:
data=reform(data(*,0:count-1))
--
Andy Bristow ajbristow@taz.dra.hmg.gb
DERA Ocean Modelling Tel: +44 (0) 1305 212323
Winfrith Technology Centre Fax: +44 (0) 1305 212103
Dorchester, DT2 8XJ, UK.
|
|
|