File Output Example
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This code example shows how to output information to an outside file directly from NetLogo code. All the data is written in human readable form using file-print
.
The example creates 15 random turtles and opens a new text file of the user's choice. Each turtle has an internal variable, value
. Then every time go
runs, it will move the turtles randomly and change value
. The turtles' patch coordinates and value
variable are stored in the opened file. You should close the file when you are finished using the FILE-CLOSE button. The data file can be viewed afterwards using any text editor.
RELATED MODELS
File Input Example
Comments and Questions
turtles-own [ value ] ;; This is some phony turtle variable ;; This procedure creates 15 random turtles and opens a file dictated ;; by the user. It will erase any pre-existing file. to setup clear-all crt 15 [ setxy random-xcor random-ycor set value 5 ] let file user-new-file ;; We check to make sure we actually got a string just in case ;; the user hits the cancel button. if is-string? file [ ;; If the file already exists, we begin by deleting it, otherwise ;; new data would be appended to the old contents. if file-exists? file [ file-delete file ] file-open file ;; record the initial turtle data write-to-file ] end ;; This procedure makes the turtles move randomly. It also changes their internal ;; variable by random +/- 1. We report the patch coordinates and their internal variable ;; to the file. To view the file, hit the file-close button and open it with a text ;; editor. to go ask turtles [ lt random 360 fd 1 + random 4 set value value + random 3 ] tick write-to-file end to write-to-file file-print (word "---------- Tick Number: " ticks "-----------") ;; use SORT so the turtles print their data in order by who number, ;; rather than in random order foreach sort turtles [ ask ? [ file-print (word self ": pxcor: " pxcor " pycor: " pycor " value: " value) ] ] file-print "" ;; blank line end ; Public Domain: ; To the extent possible under law, Uri Wilensky has waived all ; copyright and related or neighboring rights to this model.
There are 10 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
File Output Example.png | preview | Preview for 'File Output Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.