Writing 2 arrays of a different type into one text file [message #92171] |
Fri, 23 October 2015 01:10  |
Kai Heckel
Messages: 51 Registered: April 2015
|
Member |
|
|
Hello!
Is it possible to write 2 arrays into one text file?
What I have are 2 arrays: 1) float, 2) string.
I would like to write the string array into the 1st column and the float array into the 2nd column. Is this somehow possible?
Thanks in advance
Cheers,
Kai
|
|
|
Re: Writing 2 arrays of a different type into one text file [message #92172 is a reply to message #92171] |
Fri, 23 October 2015 02:25   |
Helder Marchetto
Messages: 520 Registered: November 2011
|
Senior Member |
|
|
On Friday, October 23, 2015 at 10:10:38 AM UTC+2, Kai Heckel wrote:
> Hello!
>
> Is it possible to write 2 arrays into one text file?
> What I have are 2 arrays: 1) float, 2) string.
> I would like to write the string array into the 1st column and the float array into the 2nd column. Is this somehow possible?
>
> Thanks in advance
>
> Cheers,
> Kai
Yes.
arrayFloat = randomu(s, 10)
arrayStrings = strtrim(findgen(10),2)
fileName = 'testFileOutput.txt'
get_lun, fileUnit
openw, fileUnit, fileName
printf, fileUnit, transpose(arrayStrings+', '+string(arrayFloat,FORMAT='(f0.3)'))
close, fileUnit
free_lun, fileUnit
Play a bit around with string() and the format keyword. A way or another, you will get what you want (constant width for the columns or number of decimals, separator, ...).
Cheers,
Helder
|
|
|
Re: Writing 2 arrays of a different type into one text file [message #92174 is a reply to message #92172] |
Fri, 23 October 2015 04:52   |
Kai Heckel
Messages: 51 Registered: April 2015
|
Member |
|
|
Am Freitag, 23. Oktober 2015 11:25:37 UTC+2 schrieb Helder:
> On Friday, October 23, 2015 at 10:10:38 AM UTC+2, Kai Heckel wrote:
>> Hello!
>>
>> Is it possible to write 2 arrays into one text file?
>> What I have are 2 arrays: 1) float, 2) string.
>> I would like to write the string array into the 1st column and the float array into the 2nd column. Is this somehow possible?
>>
>> Thanks in advance
>>
>> Cheers,
>> Kai
>
> Yes.
> arrayFloat = randomu(s, 10)
> arrayStrings = strtrim(findgen(10),2)
> fileName = 'testFileOutput.txt'
> get_lun, fileUnit
> openw, fileUnit, fileName
> printf, fileUnit, transpose(arrayStrings+', '+string(arrayFloat,FORMAT='(f0.3)'))
> close, fileUnit
> free_lun, fileUnit
>
> Play a bit around with string() and the format keyword. A way or another, you will get what you want (constant width for the columns or number of decimals, separator, ...).
>
> Cheers,
> Helder
Thanks Helder!
But I'd like to have the strings in the 1st column and the according float value in the 2nd column... I looked it up under FORMAT but I couldn't find the right thing. Do you have a solution for this?
Cheers,
Kai
|
|
|
Re: Writing 2 arrays of a different type into one text file [message #92175 is a reply to message #92174] |
Fri, 23 October 2015 05:13   |
Helder Marchetto
Messages: 520 Registered: November 2011
|
Senior Member |
|
|
On Friday, October 23, 2015 at 1:52:59 PM UTC+2, Kai Heckel wrote:
> Am Freitag, 23. Oktober 2015 11:25:37 UTC+2 schrieb Helder:
>> On Friday, October 23, 2015 at 10:10:38 AM UTC+2, Kai Heckel wrote:
>>> Hello!
>>>
>>> Is it possible to write 2 arrays into one text file?
>>> What I have are 2 arrays: 1) float, 2) string.
>>> I would like to write the string array into the 1st column and the float array into the 2nd column. Is this somehow possible?
>>>
>>> Thanks in advance
>>>
>>> Cheers,
>>> Kai
>>
>> Yes.
>> arrayFloat = randomu(s, 10)
>> arrayStrings = strtrim(findgen(10),2)
>> fileName = 'testFileOutput.txt'
>> get_lun, fileUnit
>> openw, fileUnit, fileName
>> printf, fileUnit, transpose(arrayStrings+', '+string(arrayFloat,FORMAT='(f0.3)'))
>> close, fileUnit
>> free_lun, fileUnit
>>
>> Play a bit around with string() and the format keyword. A way or another, you will get what you want (constant width for the columns or number of decimals, separator, ...).
>>
>> Cheers,
>> Helder
>
> Thanks Helder!
>
> But I'd like to have the strings in the 1st column and the according float value in the 2nd column... I looked it up under FORMAT but I couldn't find the right thing. Do you have a solution for this?
>
> Cheers,
> Kai
Hi Kai,
did you try the solution I gave? It gives *exactly* what you're asking for. Of course you need to have strings in the "arrayStrings" and numbers in the "arrayFloat"...
Then you get two columns, that in my case look like:
0.000000, 0.590
1.00000, 0.135
2.00000, 0.196
3.00000, 0.020
4.00000, 0.190
5.00000, 0.604
6.00000, 0.286
7.00000, 0.527
8.00000, 0.510
9.00000, 0.570
Make sure you have two arrays with the same number of elements:
IDL> help, arrayStrings
ARRAYSTRINGS STRING = Array[10]
IDL> help, arrayFloat
ARRAYFLOAT FLOAT = Array[10]
Cheers,
Helder
|
|
|
Re: Writing 2 arrays of a different type into one text file [message #92176 is a reply to message #92175] |
Fri, 23 October 2015 06:57  |
Kai Heckel
Messages: 51 Registered: April 2015
|
Member |
|
|
Am Freitag, 23. Oktober 2015 14:13:54 UTC+2 schrieb Helder:
> On Friday, October 23, 2015 at 1:52:59 PM UTC+2, Kai Heckel wrote:
>> Am Freitag, 23. Oktober 2015 11:25:37 UTC+2 schrieb Helder:
>>> On Friday, October 23, 2015 at 10:10:38 AM UTC+2, Kai Heckel wrote:
>>>> Hello!
>>>>
>>>> Is it possible to write 2 arrays into one text file?
>>>> What I have are 2 arrays: 1) float, 2) string.
>>>> I would like to write the string array into the 1st column and the float array into the 2nd column. Is this somehow possible?
>>>>
>>>> Thanks in advance
>>>>
>>>> Cheers,
>>>> Kai
>>>
>>> Yes.
>>> arrayFloat = randomu(s, 10)
>>> arrayStrings = strtrim(findgen(10),2)
>>> fileName = 'testFileOutput.txt'
>>> get_lun, fileUnit
>>> openw, fileUnit, fileName
>>> printf, fileUnit, transpose(arrayStrings+', '+string(arrayFloat,FORMAT='(f0.3)'))
>>> close, fileUnit
>>> free_lun, fileUnit
>>>
>>> Play a bit around with string() and the format keyword. A way or another, you will get what you want (constant width for the columns or number of decimals, separator, ...).
>>>
>>> Cheers,
>>> Helder
>>
>> Thanks Helder!
>>
>> But I'd like to have the strings in the 1st column and the according float value in the 2nd column... I looked it up under FORMAT but I couldn't find the right thing. Do you have a solution for this?
>>
>> Cheers,
>> Kai
>
> Hi Kai,
> did you try the solution I gave? It gives *exactly* what you're asking for. Of course you need to have strings in the "arrayStrings" and numbers in the "arrayFloat"...
> Then you get two columns, that in my case look like:
> 0.000000, 0.590
> 1.00000, 0.135
> 2.00000, 0.196
> 3.00000, 0.020
> 4.00000, 0.190
> 5.00000, 0.604
> 6.00000, 0.286
> 7.00000, 0.527
> 8.00000, 0.510
> 9.00000, 0.570
>
> Make sure you have two arrays with the same number of elements:
> IDL> help, arrayStrings
> ARRAYSTRINGS STRING = Array[10]
> IDL> help, arrayFloat
> ARRAYFLOAT FLOAT = Array[10]
>
> Cheers,
> Helder
I tried it but the separation didn't work. I solved the case by using 'WIDTH=2' when using the 'OPENW' command.
Thank you very much Helder and enjoy your weekend!
Cheers,
Kai
|
|
|