|
2 | 2 | <model version="NetLogo 7.0.3" snapToGrid="true"> |
3 | 3 | <code>extensions [ csv ] |
4 | 4 |
|
| 5 | +globals [ target-file-path ] |
| 6 | + |
5 | 7 | to setup |
6 | 8 | clear-all |
7 | 9 | file-close-all ; Close any files open from last run |
| 10 | + |
| 11 | + ; use the home-directory primitive to set a target path |
| 12 | + ; of `turtles.csv` where ever your home directory is! |
| 13 | + ; if you've never used this primitive before, you can run it |
| 14 | + ; in the command center to see what folder NetLogo thinks is your |
| 15 | + ; user's home folder on your operating system. |
| 16 | + set target-file-path (word home-directory "/turtles.csv") |
| 17 | + |
8 | 18 | reset-ticks |
9 | 19 | end |
10 | 20 |
|
|
21 | 31 | to write-turtles-to-csv |
22 | 32 | ; we use the `of` primitive to make a list of lists and then |
23 | 33 | ; use the csv extension to write that list of lists to a file. |
24 | | - csv:to-file "turtles.csv" [ (list xcor ycor size color heading) ] of turtles |
| 34 | + csv:to-file target-file-path [ (list xcor ycor size color heading) ] of turtles |
25 | 35 | end |
26 | 36 |
|
27 | 37 | ; procedure to read some turtle properties from a file |
28 | 38 | to read-turtles-from-csv |
29 | 39 | file-close-all ; close all open files |
30 | 40 |
|
31 | | - if not file-exists? "turtles.csv" [ |
32 | | - user-message "No file 'turtles.csv' exists! Try pressing WRITE-TURTLES-TO-CSV." |
| 41 | + if not file-exists? target-file-path [ |
| 42 | + user-message "No file 'turtles.csv' exists in your home directory! Try pressing WRITE-TURTLES-TO-CSV." |
33 | 43 | stop |
34 | 44 | ] |
35 | 45 |
|
36 | | - file-open "turtles.csv" ; open the file with the turtle data |
| 46 | + file-open target-file-path ; open the file with the turtle data |
37 | 47 |
|
38 | 48 | ; We'll read all the data in a single loop |
39 | 49 | while [ not file-at-end? ] [ |
|
61 | 71 | <button x="10" y="10" height="40" disableUntilTicks="false" forever="false" kind="Observer" width="190">setup</button> |
62 | 72 | <button x="10" y="152" height="40" disableUntilTicks="true" forever="false" kind="Observer" width="190">write-turtles-to-csv</button> |
63 | 73 | <button x="10" y="107" height="40" disableUntilTicks="true" forever="false" kind="Observer" width="190">generate-turtles</button> |
64 | | - <note x="50" y="62" backgroundDark="0" fontSize="14" width="200" markdown="false" height="31" textColorDark="-1" textColorLight="-16777216" backgroundLight="0">Reading/Writing |
65 | | -Turtle Data |
66 | | -</note> |
67 | 74 | <button x="10" y="242" height="40" disableUntilTicks="true" forever="false" kind="Observer" width="190">read-turtles-from-csv</button> |
68 | 75 | <button x="10" y="197" height="40" disableUntilTicks="true" forever="false" kind="Observer" width="190">ask turtles [ die ]</button> |
| 76 | + <note x="45" y="55" backgroundDark="0" fontSize="14" width="200" markdown="false" height="45" textColorDark="-1" textColorLight="-16777216" backgroundLight="0">Reading/Writing |
| 77 | +Turtle Data |
| 78 | +</note> |
69 | 79 | </widgets> |
70 | 80 | <info><![CDATA[## WHAT IS IT? |
71 | 81 |
|
72 | 82 | This model shows how to use NetLogo's `csv` extension to load data from and write data to data to CSV files. This allows users to easily output data (e.g. value of turtle variables, result of calculations, etc.) to a standard CSV file. |
73 | 83 |
|
74 | 84 | ## THINGS TO NOTICE |
75 | 85 |
|
| 86 | +We use the `home-directory` primitive to define global variable called `target-file-path` where the csv file will be written to / read from. See the CODE tab for more details. |
| 87 | +
|
76 | 88 | The GENERATE-TURTLES button just creates 100 random turtles. The user can then use the WRITE-TURTLES-TO-CSV button to save the `xcor`, `ycor`, `size`, `color` and `heading` of each turtle into a CSV file called `turtles.csv`. |
77 | 89 |
|
78 | 90 | If you were interested in generating just one line of this CSV file at a time, you could use the `csv:to-string` primitive: |
|
0 commit comments