X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 14 Jul 2006 07:15 PM by  anon
writing an array to a text file
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
14 Jul 2006 07:15 PM
    Hi all, I want to write values of two arrays to a text file. I have two arrays whose values are (1,2,3,4) and (a,b,c,d) .. these values I need to convert to text file.. the output text file should look like this... 1,2,3,4 a,b,c,d Thanks in advance kishor

    Deleted User



    New Member


    Posts:
    New Member


    --
    14 Jul 2006 07:15 PM
    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
    You are not authorized to post a reply.