Write the values of variables into a file
This functionality does not run in MATLAB.
write(<Bin | Text>, filename, <x1, x2, …>) write(n, <x1, x2, …>)
write serves for storing information from the current MuPAD® session in a file. The file contains the values of identifiers of the current session. These identifiers are assigned the stored values when this file is read into another MuPAD session via the function read.
write(filename, x1, x2, ...) stores the current values of the identifiers x1, x2 etc. to the file filename.
write(filename) stores the values of all identifiers defined in the current session to the file filename.
write(n) and write(n, x1, x2, ...) store the data in the file associated with the file descriptor n.
If the file is specified by its name, write creates a new file or overwrites an existing file; write opens and closes the file automatically.
If WRITEPATH does not have a value, write interprets the file name as a pathname relative to the "working directory".
Note that the meaning of "working directory" depends on the operating system. On Microsoft® Windows® systems and on Mac OS X systems, the "working directory" is the folder where MuPAD is installed. On UNIX® systems, it is the current working directory in which MuPAD was started; when started from a menu or desktop item, this is typically the user's home directory.
Also absolute path names are processed by write.
Instead of a file name, also a file descriptor of a file opened via fopen can be used. Cf. Example 2. In this case, the data written by write are appended to the corresponding file. The file is not closed automatically by write and must be closed by a subsequent call to fclose.
Note that fopen(filename) opens the file in read-only mode. A subsequent write command to this file causes an error. Use the Write or Append option of fopen to open the file for writing.
The file descriptor 0 represents the screen.
write stores procedures with the option noExpose in encrypted format.
Note: write stores the values of the given identifiers, not their full evaluation! Cf. Example 3.
The function is sensitive to the environment variable WRITEPATH. If this variable has a value, the file is created in the corresponding directory. Otherwise, the file is created in the "working directory."
The variable a and its value b + 1 are stored in a file named test:
a := b + 1: fid := fopen(TempFile, Write, Text): write(fid, a):
Use fname to return the name of the temporary file you created:
file := fname(fid):
The content of this file is displayed via ftextinput:
We delete the value of a. Reading the file test restores the previous value:
delete a: read(file): a
For identifiers that have no value, write writes a delete command to the file:
delete a: write(Text, 0, a):
The file test is opened for writing using the MuPAD binary format:
fid := fopen(TempFile): file := fname(fid): n := fopen(file, Write)
This number is the descriptor of the file and can be used in a write command:
a := b + 1: write(n, a): fclose(n):
delete a: read(file): a
We clean up:
delete n, a:
The value b + 1 is assigned to the identifier a. After assigning the value 2 to b, complete evaluation of a yields 3:
a := b + 1: b := 2: a
Note, however, that the value of a is the expression b + 1. This value is stored by a write command:
fid := fopen(TempFile, Write, Text): write(fid, a): file := fname(fid): ftextinput(file)
Consequently, this value is restored after reading the file into a MuPAD session:
delete a, b: read(file): a
write, when writing binary format, can store procedures with the option noExpose set. They are encrypted before writing:
f := proc(a) option noExpose; begin print(a, a^2, a*a); end_proc:
proc(a) name f; option noDebug, noExpose; begin /* Hidden */ end_proc
This is the intention behind option noExpose: You can develop code you wish not to publish, then include option noExpose in your sources, rerun your tests, use write to write a binary version of your library and distribute that.
The name of a file: a character string
x1, x2, …
A file descriptor provided by fopen: a nonnegative integer
With Bin, the data are stored in the MuPAD binary format. With Text, standard ASCII format is used. The default is Bin.
In ASCII format, assignments of the form identifier := hold(value): or delete identifier: are written into the file. See Example 1.
Void object of type DOM_NULL.