quik1399
quik1399

Reputation: 185

Import turtles position from a file

Good morning: I am currently working on a netlogo program where I have a file with the turtle coordinates and I need to import that file so each turtle adopts the position that it is in the file. The file is the following:

 9.220967873876688 30.6518906113243

 -11.68237910031844 -11.246301104888516

 2.5642482677264593 -1.6456061198786152

 24.89458409582633 22.473096145446608

 33.714972669018216 -17.295603130774897

 10.347090714821402 13.476191522153966

 3.881957027308774 -18.70063134965679

 -21.711570773095524 -25.038263308838506

 -20.649763022691737 28.674828042635635

 -5.107177490557619 21.26440747439797

 -6.29157511915799 -32.595514274136164

 19.134302620042213 -26.443694241313267

 -27.207781014142487 -3.48941202705942

 18.198639754306242 -9.202785605985115

 -12.82510430838797 5.818222632445828

 -28.761569626881588 13.521815467262908

 34.93451881825029 -34.49959417879696

 27.234341179357532 5.506201903765271

The first column is the x axis and the second column is the y axis. The world dimension is y axis (-35,35) and x axis (-35,35). In this example there are 20 coordinates. So I want to do an import from this file which generates 20 turtles placed according to the file coordinates.

Thank you for your help.

Upvotes: 1

Views: 136

Answers (1)

Matteo
Matteo

Reputation: 2926

Assuming the coordinates file is a text file with values separated by spaces and carriage returns, as it seems from the question, the file-related primitives are enough.

There are two possible cases.

You know in advance how many turtles' coordinates there will be

In this case, just use file-open, file-read and file-close:

to setup
  clear-all
  
  file-open "coordinates.txt"
  
  create-turtles 18 [
   setxy file-read file-read 
  ]
  
  file-close
end

(note that your file example contains 18 pairs of coordinates, not 20)

You don't know in advance how many turtles' coordinates there will be

In this case, add a while loop using file-at-end?:

to setup
  clear-all
  
  file-open "coordinates.txt"
  
  while [not file-at-end?] [
   create-turtles 1 [
     setxy file-read file-read
    ] 
  ]
  
  file-close
end

Upvotes: 4

Related Questions