Re: question about readf [message #49564] |
Thu, 03 August 2006 02:48 |
rlayberry
Messages: 33 Registered: November 2004
|
Member |
|
|
rlaybe...@hotmail.com wrote:
> Nicolas Decoster wrote:
>> rlayberry@hotmail.com a écrit :
>>> I am trying to get data out of MS-Excel and into IDL as a float array.
>>>
>>> I laboriously save each Excel worksheet as a csv.
>>>
>>> I then readf the CSV into a float array.
>>>
>>> The problem is that there are some missing data values. for example
>>> ,10.0,,,0.0,10
>>> so a missing data value is given by two commas next to each other.
>>> readf assumes no data is there and all my formatting goes to pot. is
>>> there anyway of getting either excel to write csvs out with dummy
>>> values (-99.9 for example) or of getting IDL to read in a missing data
>>> point as a dummy value?
>>>
>>> thanks in advance
>>>
>>> russ
>>>
>>
>> I had the same problem but with ";" instead of ",". Here is an extract
>> what I am doing :
>>
>> file = 'data.csv'
>> openr, lun, file, /get_lun
>> while not eof(lun) do begin
>> readf, lun, string
>> string = strcompress(string) ;; in case there are some
>> ;; spaces or tabs.
>> strings = strsplit(string, ',', /extract, /preserve_null)
>> n = n_elements(strings)
>> tab = fltarr(n)
>> for i = 0, n - 1 do begin
>> if (strings[i] eq '') then begin
>> tab[i] = -99.9
>> endif else begin
>> reads, strings[i], value ;; we can't use tab[i]
>> ;; directly, IDL needs
>> ;; a named variable for reads.
>> tab[i] = value
>> endelse
>> endfor
>> help, tab
>> print, tab
>> endwhile
>> free_lun, lun
>>
>> If your file contains this line: ",10.0,,,0.0,10", IDL writes:
>> TAB FLOAT = Array[6]
>> -99.9000 10.0000 -99.9000 -99.9000 0.00000 10.0000
>
> I have just been playing, and it seems to work when I do
>
> line to be read...
> 10,0,,10,10
>
> a=fltarr(6)
>
> readf,1,format='(5f)',a
>
> print,a
> 10,0,0,10
>
> I now have the probelm that I have some missing headers
>
> line to be read
> head1,head2,,head3
>
> and i want to read this into a strarr which gives head1,head2,0,head3
>
> any ideas?
sorry, just rereading your post and it has solved it with the
preserve_null...
thanks
|
|
|
Re: question about readf [message #49565 is a reply to message #49564] |
Thu, 03 August 2006 02:46  |
rlayberry
Messages: 33 Registered: November 2004
|
Member |
|
|
Nicolas Decoster wrote:
> rlayberry@hotmail.com a écrit :
>> I am trying to get data out of MS-Excel and into IDL as a float array.
>>
>> I laboriously save each Excel worksheet as a csv.
>>
>> I then readf the CSV into a float array.
>>
>> The problem is that there are some missing data values. for example
>> ,10.0,,,0.0,10
>> so a missing data value is given by two commas next to each other.
>> readf assumes no data is there and all my formatting goes to pot. is
>> there anyway of getting either excel to write csvs out with dummy
>> values (-99.9 for example) or of getting IDL to read in a missing data
>> point as a dummy value?
>>
>> thanks in advance
>>
>> russ
>>
>
> I had the same problem but with ";" instead of ",". Here is an extract
> what I am doing :
>
> file = 'data.csv'
> openr, lun, file, /get_lun
> while not eof(lun) do begin
> readf, lun, string
> string = strcompress(string) ;; in case there are some
> ;; spaces or tabs.
> strings = strsplit(string, ',', /extract, /preserve_null)
> n = n_elements(strings)
> tab = fltarr(n)
> for i = 0, n - 1 do begin
> if (strings[i] eq '') then begin
> tab[i] = -99.9
> endif else begin
> reads, strings[i], value ;; we can't use tab[i]
> ;; directly, IDL needs
> ;; a named variable for reads.
> tab[i] = value
> endelse
> endfor
> help, tab
> print, tab
> endwhile
> free_lun, lun
>
> If your file contains this line: ",10.0,,,0.0,10", IDL writes:
> TAB FLOAT = Array[6]
> -99.9000 10.0000 -99.9000 -99.9000 0.00000 10.0000
I have just been playing, and it seems to work when I do
line to be read...
10,0,,10,10
a=fltarr(6)
readf,1,format='(5f)',a
print,a
10,0,0,10
I now have the probelm that I have some missing headers
line to be read
head1,head2,,head3
and i want to read this into a strarr which gives head1,head2,0,head3
any ideas?
|
|
|
Re: question about readf [message #49567 is a reply to message #49565] |
Thu, 03 August 2006 01:59  |
Nicolas Decoster
Messages: 34 Registered: March 2000
|
Member |
|
|
rlayberry@hotmail.com a �crit :
> I am trying to get data out of MS-Excel and into IDL as a float array.
>
> I laboriously save each Excel worksheet as a csv.
>
> I then readf the CSV into a float array.
>
> The problem is that there are some missing data values. for example
> ,10.0,,,0.0,10
> so a missing data value is given by two commas next to each other.
> readf assumes no data is there and all my formatting goes to pot. is
> there anyway of getting either excel to write csvs out with dummy
> values (-99.9 for example) or of getting IDL to read in a missing data
> point as a dummy value?
>
> thanks in advance
>
> russ
>
I had the same problem but with ";" instead of ",". Here is an extract
what I am doing :
file = 'data.csv'
openr, lun, file, /get_lun
while not eof(lun) do begin
readf, lun, string
string = strcompress(string) ;; in case there are some
;; spaces or tabs.
strings = strsplit(string, ',', /extract, /preserve_null)
n = n_elements(strings)
tab = fltarr(n)
for i = 0, n - 1 do begin
if (strings[i] eq '') then begin
tab[i] = -99.9
endif else begin
reads, strings[i], value ;; we can't use tab[i]
;; directly, IDL needs
;; a named variable for reads.
tab[i] = value
endelse
endfor
help, tab
print, tab
endwhile
free_lun, lun
If your file contains this line: ",10.0,,,0.0,10", IDL writes:
TAB FLOAT = Array[6]
-99.9000 10.0000 -99.9000 -99.9000 0.00000 10.0000
|
|
|