1. Input to a script will come from a data file that has been created by another source.
2. It is useful to be able to store output in an external file that can be manipulated and/or printed later.
3. There are basically three different operations, or modes on files. Files can be: read from, written to, and appended to.
4. Writing to a file means writing to a file from the beginning. Appending to a file is also writing, but starting at the end of the file rather than the beginning.
5. Use the load function to read and the save function to write to files.
Writing Data to a File
1. The save command can be used to write data from a matrix to a data file, or to append to a data file. The format is:
save filename matvarname -ascii
The “-ascii” qualifier is used when creating a text or data file.
2. The type command can be used to display the contents of the file.
3. Note that if the file already exists, the save command will overwrite the file.
4. Save always writes from the beginning of a file.
Appending Data to a Data File
Once a text file exists, data can be appended to it. The format is the same as the preceding, with the addition of the
qualifier “-append”. Once a text file exists, data can be appended to it. The format is the same as the preceding, with the addition of the qualifier “-append”.
Reading from a File
1. It is accomplished using load. Once a file has been created, it can be read into a matrix variable.
2. If the file is a data file, the load command will read from the file “filename.ext”
3. The load command works only if there are the same number of values in each line so that the data can be stored in a matrix, and the save command only writes from a matrix to a file.