X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 28 Feb 2011 04:51 PM by  anon
Reading data from a txt file and then using it
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
28 Feb 2011 04:51 PM
    Hi, I'm not quite sure what is going wrong in my code so hoping for some help please. I have a .txt file that has been output from a different program which I want to read into IDL. The file itself looks like: x 1 circle 2 square 3 square 4 circle The first line which has an 'x' is just a dummy line that the program puts in and can't change. The commands I'm using to open and read the file are: openr, 1, 'filename.txt' readf, 1, object, shape The idea of my program is that I have a "for loop" over the numbered section (i.e. for object=1,4 do begin) then in the loop I take the associated tag (in this case, for object=1, I am wanting to do some commands with the word circle, for instance: if shape eq 'circle' then shape=1 if shape eq 'triangle' then shape=2 I try to run this but IDL doesn't like this and prints : % READF: Input conversion error. Unit: 1, File: filename.txt Cheers in advance for any help given

    Deleted User



    New Member


    Posts:
    New Member


    --
    24 Mar 2011 04:21 PM
    Hi trekker_colin, I think the following example code is what you are looking for: pro readingtext shapesfile='C:\path\to\file\test_texts.txt' openr, lun, shapesfile, /GET_LUN ; Read one line at a time, saving the result into array s='' object = intarr(4) shape = strarr(4) i=0 WHILE NOT EOF(lun) DO BEGIN READF, lun, n, s, format='(i1,1x,a6)' object[i]=n shape[i]=s i=i+1 ENDWHILE help, shape print, shape for i=0, 3 do begin if (shape[i] eq 'circle') then shape[i]=1 if (shape[i] eq 'square') then shape[i]=2 endfor print, shape ; Close the file and free the file unit FREE_LUN, lun end Notice that this example is hardcoded so that the name strings have the same number of characters, i.e. 6. Cheers, Fernando
    You are not authorized to post a reply.