1.11.1 Reading and Writing Mathematica Files
You can use files on your computer system to store definitions and results from Mathematica. The most general approach is to store everything as plain text that is appropriate for input to Mathematica. With this approach, a version of Mathematica running on one computer system produces files that can be read by a version running on any computer system. In addition, such files can be manipulated by other standard programs, such as text editors.
Reading and writing files.
|<< name ||read in a Mathematica input file |
|expr >> name ||output expr to a file as plain text |
|expr >>> name ||append expr to a file |
|!!name ||display the contents of a plain text file |
|This expands , and outputs the result to a file called tmp. || |
Expand[ (x + y)^3 ] >> tmp
|Here are the contents of tmp. They can be used directly as input for Mathematica. || |
|This reads in tmp, evaluating the Mathematica input it contains. || |
If you are familiar with command-line operating systems, you will recognize the Mathematica redirection operators >>, >>> and << as being analogous to the command-line operators >, >> and <.
The redirection operators >> and >>> are convenient for storing results you get from Mathematica. The function Save["name", f, g, ... ] allows you to save definitions for variables and functions.
Saving definitions in plain text files.
|Save["name", f, g, ... ] ||save definitions for variables or functions in a file |
|Here is a definition for a function f. || |
|This gives c the value 17. || |
|This saves the definition of f in the file ftmp. || |
|Mathematica automatically saves both the actual definition of f, and the definition of c on which it depends. || |
|This clears the definitions of f and c. || |
|You can reinstate the definitions you saved simply by reading in the file ftmp. || |
Typical names of Mathematica files.
|file.m ||Mathematica expression file in plain text format |
|file.nb ||Mathematica notebook file |
|file.mx ||Mathematica definitions in DumpSave format |
If you use a notebook interface to Mathematica, then the Mathematica front end allows you to save complete notebooks, including not only Mathematica input and output, but also text, graphics and other material.
It is conventional to give Mathematica notebook files names that end in .nb, and most versions of Mathematica enforce this convention.
When you open a notebook in the Mathematica front end, Mathematica will immediately display the contents of the notebook, but it will not normally send any of these contents to the kernel for evaluation until you explicitly request this to be done.
Within a Mathematica notebook, however, you can use the Cell menu in the front end to identify certain cells as initialization cells, and if you do this, then the contents of these cells will automatically be evaluated whenever you open the notebook.
|The I in the cell bracket indicates that the second cell is an initialization cell that will be evaluated whenever the notebook is opened. || |
It is sometimes convenient to maintain Mathematica material both in a notebook which contains explanatory text, and in a package which contains only raw Mathematica definitions. You can do this by putting the Mathematica definitions into initialization cells in the notebook. Every time you save the notebook, the front end will then allow you to save an associated .m file which contains only the raw Mathematica definitions.