How to save data in different column in csv format for each 5 ticks.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to save data in different column in csv format for each 5 ticks.

Abeni
I want to export the x-cor, y-cor and heading angle data of the agents to CSV
file for every 5 ticks. I tried to use the example given in the NetLogo
library but it only exports the first tick data and stops(only one column).
Anyone could share me your idea or code if you already have done it.

Thanks



--
Sent from: http://netlogo-users.18673.x6.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: How to save data in different column in csv format for each 5 ticks.

Bryan Head
You can use the built-in csv
<https://ccl.northwestern.edu/netlogo/docs/csv...html> extension for this!
Here’s the basics of how to do that:

In setup, you want to open your output file and write your column headings:

to setup
  ca
  ...
  file-open "output.csv"
  file-print csv:to-row (list "id" "xcor" "ycor" "heading")
  ...
  reset-ticks
end

Note I’m adding turtle ids to the output to make it easier to track which
row corresponds to which turtle.

Then, add procedure to do the output the data:

to output-turtle-data
  ask turtles [
    file-print csv:to-row (list who xcor ycor heading)
  ]
  file-flush ; Make sure that the output is saved to file
end

Call this procedure from go

to go
  ...
  output-turtle-data
  ...
  tick
end

If you want to make it only output every 5 ticks, do if ticks mod 5 = 0 [
output-turtle-data ] instead!

You can also do this without the csv extension, but it’s a little messier
as you have to manage columns yourself. The header output in setup
becomes: file-print
"id,xcor,ycor,heading" and the print statement in output-turtle-data
becomes: file-print (word who "," xcor "," ycor "," heading)

Hope that helps!
Bryan