Create an array for data opend and read from text file [message #94164] |
Thu, 09 February 2017 20:44  |
smnadoum
Messages: 24 Registered: June 2016
|
Junior Member |
|
|
Hi,
I might be over thinking this but I could use some help. I wrote this code that opens and reads data from a text file. I am trying to create an array for the data that was opened, read, and printed, however, my attempts for creating an array for the data are all unsuccessful: mwell=float(x, logx, y) . Can anyone identify the problem?
pro read_text, file, dir
dir='C:\Users\shereen\Videos'
file= 'C:\Users\shereen\Videos\test.txt'
n = file_lines(file)
x= fltarr(n)
y= fltarr(n)
logx= fltarr(n)
x0= 0.0
logx0= 0.0
y0=0.0
openr, iunit, file, /get_lun
for i= 0, n-1 do begin
readf, iunit, x0, logx0, y0
x[i]= x0
logx[i]= logx0
y[i]=[y0]
endfor
free_lun, iunit
for i = 0, n-1 do print, x[i], logx[i], y[i]
;;idl prints
0.115825 0.251150 0.344600
0.0822174 0.266348 0.406000
0.0674909 0.246855 0.531873
0.0985870 0.225174 0.506348
0.117978 0.189022 0.541152
0.119739 0.196804 0.457326
0.137261 0.221652 0.426978
0.128391 0.209696 0.400348
0.132717 0.247109 0.365717
0.103391 0.229152 0.312783
0.122043 0.221109 0.371478
0.0880652 0.277435 0.365022
0.157629 0.179771 0.360571
0.256195 0.256195 0.441024
0.262326 0.262326 0.454457
0.229582 0.229582 0.529491
0.185870 0.185870 0.588435
0.160543 0.160543 0.542522
0.178348 0.178348 0.531804
0.206630 0.206630 0.452717
0.239457 0.239457 0.405826
0.242913 0.242913 0.349891
0.277283 0.277283 0.365630
0.244478 0.244478 0.413913
0.225174 0.225174 0.478543
0.207629 0.207629 0.449314
0.389293 0.245756 0.389293
0.408196 0.222978 0.408196
0.433745 0.245418 0.433745
0.493109 0.219761 0.493109
0.435891 0.227109 0.435891
0.456435 0.257217 0.456435
0.450022 0.231522 0.450022
0.408457 0.235239 0.408457
0.392261 0.212826 0.392261
0.445522 0.261696 0.445522
0.413543 0.234130 0.413543
0.415348 0.217891 0.415348
0.441343 0.210971 0.441343
mwell=float(x, logx, y) ;I want to create an array for the printed data without changing the size, dimension, etc..
end
Thank you.
|
|
|
Re: Create an array for data opend and read from text file [message #94165 is a reply to message #94164] |
Fri, 10 February 2017 07:00  |
Dick Jackson
Messages: 347 Registered: August 1998
|
Senior Member |
|
|
On Thursday, 9 February 2017 20:44:07 UTC-8, Cheryl wrote:
> Hi,
>
> I might be over thinking this but I could use some help. I wrote this code that opens and reads data from a text file. I am trying to create an array for the data that was opened, read, and printed, however, my attempts for creating an array for the data are all unsuccessful: mwell=float(x, logx, y) . Can anyone identify the problem?
>
>
> pro read_text, file, dir
>
> dir='C:\Users\shereen\Videos'
> file= 'C:\Users\shereen\Videos\test.txt'
>
> n = file_lines(file)
> x= fltarr(n)
> y= fltarr(n)
> logx= fltarr(n)
> x0= 0.0
> logx0= 0.0
> y0=0.0
>
>
> openr, iunit, file, /get_lun
>
> for i= 0, n-1 do begin
>
> readf, iunit, x0, logx0, y0
>
> x[i]= x0
>
> logx[i]= logx0
>
> y[i]=[y0]
>
> endfor
>
>
> free_lun, iunit
>
> for i = 0, n-1 do print, x[i], logx[i], y[i]
> ;;idl prints
> 0.115825 0.251150 0.344600
> 0.0822174 0.266348 0.406000
> […]
> 0.441343 0.210971 0.441343
>
> mwell=float(x, logx, y) ;I want to create an array for the printed data without changing the size, dimension, etc..
>
> end
>
>
> Thank you.
Hi Cheryl,
The array I think you are trying to create would be:
mwell = Float(3, n)
; and then,
mwell[0, *] = x
mwell[1, *] = logx
mwell[2, *] = y
But since you know the structure of the file, the whole thing could be simplified (in the "IDL Way") as:
pro read_text, file, dir
dir='C:\Users\shereen\Videos'
file= 'C:\Users\shereen\Videos\test.txt'
n = file_lines(file)
mwell = Float(3, n)
openr, iunit, file, /get_lun
readf, iunit, mwell
free_lun, iunit
end
Another option is to use an array of structures, where you would just define mwell as:
mwell = Replicate({x:0.0, logx:0.0, y:0.0}, n)
(all else remains the same!)
… and then you can use "mwell.x" to refer to the array of 'x' values, which is clearer than using "mwell[0, *]". Same goes for mwell.logx and mwell.y.
I hope this helps!
Cheers,
-Dick
Dick Jackson Software Consulting Inc.
Victoria, BC, Canada --- http://www.d-jackson.com
|
|
|