Download NetLogo 2.1.0 User Manual
Transcript
NetLogo 2.1.0 User Manual • write lets you print values in a format which can be read back in using file−read. A NetLogo model may optionally have an "output area" in its Interface tab, separate from the Command Center. To send output there instead of the Command Center, use the output−print, output−show, output−type, and output−write commands. The output area can be cleared with the clear−output command and saved to a file with export−output. If you use output−print, output−show, output−type, output−write, clear−output, or export−output in a model which does not have a separate output area, then the commands apply to the output portion of the Command Center. File I/O In NetLogo, there is a set of primitives that give you the power to interact with outside files. They all begin with the prefix file−. There are two main modes when dealing with files: reading and writing. The difference is the direction of the flow of data. When you are reading in information from a file, data that is stored in the file flows into your model. On the other hand, writing allows data to flow out of your model and into a file. When a NetLogo model runs as an applet within a web browser, it will only be able to read data from files which are in the same directory on the server as the model file. Applets cannot write to any files. When working with files, always begin by using the primitive file−open. This specifies which file you will be interacting with. None of the other primitives work unless you open a file first. The next file− primitive you use dictates which mode the file will be in until the file is closed, reading or writing. To switch modes, close and then reopen the file. The reading primitives include file−read, file−read−line, file−read−characters, and file−at−end? Note that the file must exist already before you can open it for reading. The primitives for writing are similar to the primitives that print things in the Command Center, except that the output gets saved to a file. They include file−print, file−show, file−type, and file−write. Note that you can never "overwrite" data. In other words, if you attempt to write to a file with existing data, all new data will be appended to the end of the file. (If you want to overwrite a file, use file−delete to delete it, then open it for writing.) When you are finished using a file, you can use the command file−close to end your session with the file. If you wish to remove the file afterwards, use the primitive file−delete to delete it. To close multiple opened files, one needs to first select the file by using file−open before closing it. ;; Open 3 file−open file−open file−open files "myfile1.txt" "myfile2.txt" "myfile3.txt" Programming Guide 89