3

I have a question about the number of turtles created from this code :

to read-turtles-from-csv
  file-close-all ; close all open files
  if not file-exists? "turtles.csv" [
    user-message "No file 'turtles.csv' exists! Try pressing WRITE-TURTLES-TO-CSV."
    stop
  ]
  file-open "turtles.csv" ; open the file with the turtle data

  ; We'll read all the data in a single loop
  while [ not file-at-end? ] [

    let data csv:from-row file-read-line

    create-turtles 1 [
     print "item column 4"
     show item 4 data







    ]
  ]

  file-close ; make sure to close the file
end

My turtles.csv file has only two rows, so I What is expected here is that create-turtles 1 is repeated for the number of rows and I have two agents for which 2 numbers in the 4th column get printed. Surprisingly though, 4 turtles are created! Why?

Thanks

user710
  • 161
  • 2
  • 12

1 Answers1

2

I'm wondering if your turtles.csv is being read in as having more lines than it should? Try doing something like:

to read-file
  file-close-all
  file-open "turtles.csv"
  while [not file-at-end?] [
    print csv:from-row file-read-line
  ]
  file-close-all
end

to see how Netlogo is reading the file. It seems like you're on the right track, otherwise- I just tested a similar file following your example and I got my two turtles as expected. Using a .csv file called "turtle_details.csv" that looks like:

  color size heading
1   red    2      90
2  blue    4     180

I used this code to produce two turtles with the variables in the .csv:

extensions [ csv ]

to setup
  ca
  reset-ticks
  file-close-all 
  file-open "turtle_details.csv"

  ;; To skip the header row in the while loop,
  ;  read the header row here to move the cursor
  ;  down to the next line.
  let headings csv:from-row file-read-line 

  while [ not file-at-end? ] [
    let data csv:from-row file-read-line
    print data
    create-turtles 1 [
      set color read-from-string item 0 data
      set size item 1 data
      set heading item 2 data
    ]
  ]
  file-close-all  
end
Luke C
  • 10,081
  • 1
  • 14
  • 21
  • 1
    Thanks so much Luke. I had made a mistake that had lead to more turtles. The dataset had 2 rows, but the cursor was at the beginning of the 5th row, so it created 5 turtles for me! It does work this way and creates the appropriate number of turtles. Just in your code, in the section about setting color, size, and heading, the numer of items should change to 1,2, and 3 respectively, so it will work perfectly accurate. Thank you so much for letting me know how to skip the header. It was extremely helpful. – user710 Jun 27 '17 at 16:46
  • No worries! The `.csv` in my example does not actually include the row numbers 1 and 2- that is a holdover from other software I pasted from. That is why I used 0, 1, and 2 for the `item` indexing. As a side note, I think if you assign the header to a variable, you might be able to sort out your [earlier question](https://stackoverflow.com/questions/44769763/assign-values-to-turtles-by-searching-through-csv-header-row-in-netlogo) by comparing a turtle value to the header values and using that to create an index for your variable assignment. – Luke C Jun 27 '17 at 16:53
  • Thanks so much for the great idea. I will try that. – user710 Jun 28 '17 at 00:40
  • Thank you vey much. The idea about assigning a variable to the header worked pretty well, but still there are other problems that I asked it as a new question. – user710 Jun 29 '17 at 18:09