If you can get IDL's rudimentary PRINT command to write those values out to the IDL Output Log the way you want them to appear, then it is one easy step further to output that same formatting to a file. There are several options for formatting the string representation of an IDL array as a single line of comma-delimited entries. Here is one easy way using intuitive, but not the most efficient code:
; Make the integer array [1,2,3,4]
x = indgen(4)
; Convert x to an array of text numbers with no whitespace
xAsString = strtrim(x, 2)
; Make one long string out of the converted array with comma delimiter
outputLine = strjoin(xAsString, ',')
print, outputLine
; 0,1,2,3
However, the true IDL programmer's approach would be to use the appropriate format code setting for PRINT's FORMAT keyword, as in:
; Demonstrating how this can be done dynamically for different array sizes
nElements = 4
x = indgen(nElements)
; You need the number of array elements in the format code!
formatCode = '(' + strtrim(nElements,2) + '(i0, :, ",")'
print, formatCode ; So you can see how this format coding looks
; (4(i0, :, ","))
print, x, FORMAT=formatCode
0,1,2,3
That was the hard part. Outputting the above text to a file is the easy part.
openw, lun, 'my_ascii_data.txt', /GET_LUN ; Routine file opening command
printf, lun, FORMAT=formatCode
; ... or "printf, lun, outputLine", if you are using the first algorithm
free_lun, lun ; Routine file closing command
James Jones
|