X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 26 Jul 2007 03:03 AM by  anon
How to count the number of columns in an input file?
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
26 Jul 2007 03:03 AM
    I use IDL to read large test files containing data. I am using the function file_lines() to count the number of lines of data in an input file. Is there a similar function to count the number of columns of data in an input file? Thanks.

    Deleted User



    New Member


    Posts:
    New Member


    --
    26 Jul 2007 03:03 AM
    Dear Andrew, There is a way to count the number of columns. The algorithm will be to read in the first line of data and then use STRSPLIT and then to count the number of elements created by STRSPLIT. The following example ilustrate this algorithm: pro counting_columns file='countingcolumns.dat' openr, lun, file, /get_lun nlines=File_Lines(file) ; Read the first line and parse it into column units. line="" ReadF, lun, line ncolumns = N_Elements(STRSPLIT(line, /RegEx, /EXTRACT)) print, ncolumns data=lonarr(ncolumns,nlines) ;Rewind the data file to its start reading the data into the array 'data'. Point_Lun, lun, 0 ;Read the data. ReadF, lun, data plot, data(0,*), data(1,*) Free_Lun, lun end The rewind line in the code is there to assure that you will start to read the file from the beginning when you are ready to put the data into an array. In my case the input file is an example of and ASCII file composed of 3 columns and 5 lines. I hope this helped.
    You are not authorized to post a reply.