File Input 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 read in information from a file directly from NetLogo code.
In this example, we use the data to make a complicated patch maze. load-patch-data
will load in patch data from a file to a list, where show-patch-data
will display all the data that was loaded in the view. By default, there is only one file that is included that can be loaded in. It is called "File IO Patch Data.txt" and it is located in the code example's directory. You can use load-own-patch-data
to see how one would let the user decide which file to choose. The function save-patch-data
is in the procedures if you wish to see how the file "File IO Patch Data.txt" was created. For more information about file output, see File Output Example.
File input can be used to load in complicated information or to give the user the option to choose data. A good example is loading in patch information (such a maze in this case), or turtle information (such as coordinates). The difference between doing this and import-world
is that the user can customize the way the data is imported or exported -- you can save or load only the relevant data.
RELATED MODELS
File Output Example
Comments and Questions
globals [ patch-data ] ;; This procedure loads in patch data from a file. The format of the file is: pxcor ;; pycor pcolor. You can view the file by opening the file File IO Patch Data.txt ;; using a simple text editor. Note that it automatically loads the file "File IO ;; Patch Data.txt". To have the user choose their own file, see load-own-patch-data. to load-patch-data ;; We check to make sure the file exists first ifelse ( file-exists? "File IO Patch Data.txt" ) [ ;; We are saving the data into a list, so it only needs to be loaded once. set patch-data [] ;; This opens the file, so we can use it. file-open "File IO Patch Data.txt" ;; Read in all the data in the file while [ not file-at-end? ] [ ;; file-read gives you variables. In this case numbers. ;; We store them in a double list (ex [[1 1 9.9999] [1 2 9.9999] ... ;; Each iteration we append the next three-tuple to the current list set patch-data sentence patch-data (list (list file-read file-read file-read)) ] user-message "File loading complete!" ;; Done reading in patch information. Close the file. file-close ] [ user-message "There is no File IO Patch Data.txt file in current directory!" ] end ;; This procedure does the same thing as the above one, except it lets the user choose ;; the file to load from. Note that we need to check that it isn't false. This because ;; it will return false if the user cancels the file dialog. There is currently only ;; one file to load from, but you can create your own using the function save-patch-data ;; near the bottom which saves all the current patches into a file. to load-own-patch-data let file user-new-file if ( file != false ) [ set patch-data [] file-open file while [ not file-at-end? ] [ set patch-data sentence patch-data (list (list file-read file-read file-read)) ] user-message "File loading complete!" file-close ] end ;; This procedure will use the loaded in patch data to color the patches. ;; The list is a list of three-tuples where the first item is the pxcor, the ;; second is the pycor, and the third is pcolor. Ex. [ [ 0 0 5 ] [ 1 34 26 ] ... ] to show-patch-data cp ct ifelse ( is-list? patch-data ) [ foreach patch-data [ ask patch first ? item 1 ? [ set pcolor last ? ] ] ] [ user-message "You need to load in patch data first!" ] end ;; This is the procedure that was used to create the file "File IO Patch Data.txt". ;; You can also use it to create your own files. See File Output code example for more ;; details on File Output. to save-patch-data let file user-new-file if ( file != false ) [ file-write file ask patches [ file-write pxcor file-write pycor file-write pcolor ] file-close ] 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 Input Example.png | preview | Preview for 'File Input Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.