Legacy Documentation

Mathematica® Teacher's Edition (2002)

This is documentation for an obsolete product.
Current products and services
 Documentation /  Mathematica Teacher's Edition /  The Teacher's Book /  Interlude 3: Other Capabilities /

30.1 Reading and Writing Mathematica TE Files

On systems with notebook-based interfaces you will usually find it easier to save whole notebooks rather than saving specific Mathematica TE expressions and definitions. The standard notebook interface for Mathematica TE allows you to specify certain cells of Mathematica TE input as being "initialization cells". These cells can contain definitions and results that are automatically set up again whenever you open the notebook.
You can also use files on your computer system to store definitions and results from Mathematica TE. The most general approach is to store everything as plain text that is appropriate for input to Mathematica TE. With this approach, a version of Mathematica TE 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.

This expands and outputs the result to a file called tmp.

In[1]:= Expand[ (x + y)^3 ] >> tmp

Here are the contents of tmp. They can be used directly as input for Mathematica TE.

In[2]:= !!tmp

x^3 + 3*x^2*y + 3*x*y^2 + y^3

This reads in tmp, evaluating the Mathematica TE input it contains.

In[3]:= <<tmp

Out[3]=

The redirection operators >> and >>> are convenient for storing results you get from Mathematica TE. The function Save["name", f, g, ... ] allows you to save definitions for variables and functions.

Saving definitions in plain text files.

Here is a definition for a function f.

In[4]:= f[x_] := x^2 + c

This gives c the value 17.

In[5]:= c = 17

Out[5]=

This saves the definition of f in the file ftmp.

In[6]:= Save["ftmp", f]

Mathematica TE automatically saves both the actual definition of f, and the definition of c on which it depends.

In[7]:= !!ftmp

f[x_] := x^2 + c

c = 17

This clears the definitions of f and c.

In[8]:= Clear[f, c]

You can reinstate the definitions you saved simply by reading in the file ftmp.

In[9]:= <<ftmp

Out[9]=

Mathematica options related to the file system. (See Section 3.2.)