Write values of variables into file
This functionality does not run in MATLAB.
write(<Bin | Text>, <Encoding = "encodingValue">, filename, <x1, x2, …>) write(<Encoding = "encodingValue">, 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.
write(..., Encoding = "encodingValue", ...) stores the current values of identifiers in the specified encoding only when writing in Text mode. For the supported encodings, see Options. You can use this option with any of the previously specified syntaxes.
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 folder".
Note that the meaning of "working folder" depends on the operating system. On Microsoft® Windows® systems and on Mac OS X systems, the "working folder" is the folder whereMATLAB® is installed. On UNIX® systems, it is the current working folder in which MATLAB was started. When started from a menu or desktop item, this is typically the user's home folder.
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 folder. Otherwise, the file is created in the "working folder."
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:
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
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.
To specify the encoding to write data, use Encoding. The Encoding option applies only to text files that are opened using a file name and not a file descriptor. Write the value of the identifier a:="abcäöü" into a temporary file in the encoding "UTF-8":
Specify the correct encoding to read the file:
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.
This option lets you specify the character encoding to use. The allowed encodings are:
The default encoding is system dependent. If you specify the encoding incorrectly, characters might read incorrectly. Characters unrecognized by the encoding are replaced by the default substitution character for the specified encoding.
Encodings not listed here can be specified but might not produce correct results.
Void object of type DOM_NULL.