comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Create an array for data opend and read from text file
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
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 Go to previous message
Dick Jackson is currently offline  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
[Message index]
 
Read Message
Read Message
Previous Topic: IDL installation in mac OS X 10.11
Next Topic: TV type routine that can use irregular x and y coordinates?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 23:35:13 PDT 2025

Total time taken to generate the page: 0.80485 seconds