Documentation

write

Write values of variables into file

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

write(<Bin | Text>, <Encoding = "encodingValue">, filename, <x1, x2, …>)
write(<Encoding = "encodingValue">, n, <x1, x2, …>)

Description

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. See 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! See Example 3.

Environment Interactions

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."

Examples

Example 1

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:

ftextinput(file)

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):
delete a:

Example 2

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

Clean up:

delete n, a:

Example 3

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

delete a:

Example 4

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:
write("hidden_proc.mb", f):
delete f:
read("hidden_proc.mb"):
f(-2...3);
expose(f)

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.

Example 5

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":

a:="abcäöü":
write(Text,Encoding="UTF-8","write_test",a):

Specify the correct encoding to read the file:

read("write_test", Encoding="UTF-8")

If you do not specify an encoding, the default system encoding is used. Thus, your output might vary from that shown next. Characters unrecognized by the default system encoding are replaced by the default substitution character for that encoding:

a:="abcäöü":
write(Text, "write_test", a):
read("write_test")

Parameters

filename

The name of a file: a character string

x1, x2, …

identifiers

n

A file descriptor provided by fopen: a nonnegative integer

Options

Bin, Text

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.

Encoding

This option lets you specify the character encoding to use. The allowed encodings are:

"Big5"

"ISO-8859-1"

"windows-932"

"EUC-JP"

"ISO-8859-2"

"windows-936"

"GBK"

"ISO-8859-3"

"windows-949"

"KSC_5601"

"ISO-8859-4"

"windows-950"

"Macintosh"

"ISO-8859-9"

"windows-1250"

"Shift_JIS"x

"ISO-8859-13"

"windows-1251"

"US-ASCII"

"ISO-8859-15"

"windows-1252"

"UTF-8"

 

"windows-1253"

  

"windows-1254"

  

"windows-1257"

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.

Return Values

Void object of type DOM_NULL.

Was this topic helpful?