Skip to content

Commit a5a9f15

Browse files
committed
update csv example to use home-directory
1 parent bc562cb commit a5a9f15

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Code Examples/Extensions Examples/csv/CSV Example.nlogox

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22
<model version="NetLogo 7.0.3" snapToGrid="true">
33
<code>extensions [ csv ]
44

5+
globals [ target-file-path ]
6+
57
to setup
68
clear-all
79
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+
818
reset-ticks
919
end
1020

@@ -21,19 +31,19 @@ end
2131
to write-turtles-to-csv
2232
; we use the `of` primitive to make a list of lists and then
2333
; 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
2535
end
2636

2737
; procedure to read some turtle properties from a file
2838
to read-turtles-from-csv
2939
file-close-all ; close all open files
3040

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."
3343
stop
3444
]
3545

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
3747

3848
; We'll read all the data in a single loop
3949
while [ not file-at-end? ] [
@@ -61,18 +71,20 @@ end
6171
<button x="10" y="10" height="40" disableUntilTicks="false" forever="false" kind="Observer" width="190">setup</button>
6272
<button x="10" y="152" height="40" disableUntilTicks="true" forever="false" kind="Observer" width="190">write-turtles-to-csv</button>
6373
<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>
6774
<button x="10" y="242" height="40" disableUntilTicks="true" forever="false" kind="Observer" width="190">read-turtles-from-csv</button>
6875
<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>
6979
</widgets>
7080
<info><![CDATA[## WHAT IS IT?
7181
7282
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.
7383
7484
## THINGS TO NOTICE
7585
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+
7688
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`.
7789
7890
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

Comments
 (0)