fprint

Write data to file

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

fprint(<Unquoted | NoNL>, <Bin | Text>, <Encoding = "encodingValue">, filename, <object1, object2, …>)
fprint(<Unquoted | NoNL>, <Encoding = "encodingValue">, n, <object1, object2, …>)

Description

fprint(f, objects) writes MuPAD® objects to the file f. The objects are evaluated, the results are stored in the file. These data can be read into another MuPAD session via the functions finput and ftextinput, respectively.

fprint(Encoding = "encodingValue", f, objects) uses the specified encoding. For supported encodings, see Options.

The file may be specified directly by its name. In this case, fprint creates a new file or overwrites an existing file. fprint opens and closes the file automatically.

If WRITEPATH does not have a value, fprint interprets the file name as a path name relative to the “working folder.”

Note that the meaning of “working folder” depends on the operating system. On Windows® systems and on Mac OS X systems, the “working folder” is the folder where MATLAB® 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 fprint.

If the filename given ends in “.gz”, MuPAD automatically writes a compressed file in gzip format. These files are transparently uncompressed when read in again by MuPAD. The gzip format is supported by many other programs as well. See Example 5.

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 fprint are appended to the corresponding file. The file is not closed automatically by fprint and must be closed by a subsequent call to fclose.

Note that fopen(filename) opens the file in read-only mode. A subsequent fprint command to this file causes an error. Use the Write or Append option of fopen to open the file for writing.

Note

  The file descriptor 0 represents the screen. See Example 4.

Text output occurs without the Pretty-Printer. A call to fprint writes all specified objects into a single line of the text file. A newline character is appended to this line, unless the option NoNL is used. By default, the written objects are separated by colons without any further white space. The resulting text data consists of syntactically correct MuPAD code and can be read again using finput. With the options Unquoted and NoNL, neither white space no colons are inserted to separate the objects. The resulting text data cannot be read again using finput. 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

Write some data to the file “test”. By default, this file is created as a binary file:

fid := fopen(TempFile, Write, Text):
d := 5:
fprint(fid, d, d*3):
file := fname(fid):
fclose(fid)

The file is read into the MuPAD session:

finput(file, e, f): d, e, f;

delete d, e, f:

Example 2

Use a file descriptor to access the file test. Several calls to fprint append data to the file:

n := fopen(file, Write): 
fprint(n, (d := 5), d*3): 
fprint(n, "more data"):

Using a file descriptor, call fclose to close the file:

fclose(n):

The file is read into the MuPAD session, assigning the stored values to the identifiers e, f, and g:

finput(file, e, f, g ): e, f, g;

delete n, d, e, f, g:

Example 3

With the option Unquoted, character strings are written without quotation marks:

fid1 := fopen(TempFile):
fid2 := fopen(TempFile):
file1 := fname(fid1):
file2 := fname(fid2):
fprint(Text, file1, "Hello World!", MuPAD + 1):
fprint(Unquoted, Text, file2, "Hello World!", MuPAD + 1):

Creates temporary files have the following content:

"Hello World!":MuPAD + 1:

Hello World!MuPAD + 1

Use finput or ftextinput to read the data from the file:

finput(file1, a, b):
a, b;

ftextinput(file2, c): c

delete a, b, c:

Example 4

Typically, the print function serves for displaying objects on screen. If the object produces a line that is longer than the TEXTWIDTH setting, print breaks that line into shorter lines and inserts the line continuation characters. To avoid inserting line continuation characters, display long objects on screen by using the fprint function with the file descriptor 0. For example, convert the following expression to a TeX formatted string. When you use the print function, the resulting string contains the line continuation character (\):

print(Unquoted, generate::TeX(diff(1/ln(1/x), x$4)))
\frac{22}{x^4\,{\ln\left(\frac{1}{x}\right)}^3}-\frac{6}{x^4\,{\ln\left(\
\frac{1}{x}\right)}^2}-\frac{36}{x^4\,{\ln\left(\frac{1}{x}\right)}^4}+\fr\
ac{24}{x^4\,{\ln\left(\frac{1}{x}\right)}^5}

If you want to use the generated string in TeX, you must remove these additional characters. Also, you can generate the string without these characters by using the fprint function:

fprint(Unquoted, 0, generate::TeX(diff(1/ln(1/x), x$4)))
\frac{22}{x^4\,{\ln\left(\frac{1}{x}\right)}^3}-\frac{6}{x^4\,{\ln\left(\frac{1}{x}\right)}^2}-\frac{36}{x^4\,{\ln\left(\frac{1}{x}\right)}^4}+\frac{24}{x^4\,{\ln\left(\frac{1}{x}\right)}^5}

Another way to avoid line continuation characters is to increase the TEXTWIDTH setting:

defaultWidth := TEXTWIDTH:
TEXTWIDTH := 250:
print(Unquoted, generate::TeX(diff(1/ln(1/x), x$4)));
TEXTWIDTH := defaultWidth:
\frac{22}{x^4\,{\ln\left(\frac{1}{x}\right)}^3}-\frac{6}{x^4\,{\ln\left(\frac{1}{x}\right)}^2}-\frac{36}{x^4\,{\ln\left(\frac{1}{x}\right)}^4}+\frac{24}{x^4\,{\ln\left(\frac{1}{x}\right)}^5}

Example 5

When writing to a file with a name ending in “.gz”, MuPAD creates a compressed file automatically. On a UNIX system, the file command can be used to verify this:

fprint(Text, "test.gz", "test"):
system("file test.gz"):
test.gz: gzip compressed data, from Unix

Reading the file from MuPAD does not show a difference, because gzip-compressed files are automatically uncompressed in memory by MuPAD:

ftextinput("test.gz")

Example 6

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. Create a file and write the string "abcäöü" in the encoding "UTF-8":

fprint(Unquoted, Text, Encoding="UTF-8", "fprint_test", "abcäöü"):

Use ftextinput to read the data with the specified encoding:

ftextinput("fprint_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:

fprint(Unquoted, Text, "fprint_test", "abcäöü"):
ftextinput("fprint_test")

Parameters

filename

The name of a file: a character string

object1, object2, …

Arbitrary MuPAD objects

n

A file descriptor provided by fopen: a nonnegative integer

Options

Unquoted

With this option, character strings are displayed without quotation marks. Moreover, the control characters '\n' (or '\r\n' in Windows), '\t', and '\\' in strings are expanded into a new line, a tabulator skip, and a single backslash \, respectively. Furthermore, no colons are inserted between the objects. A newline character is appended to the line written by fprint.

This option is relevant for text files only. It is useful for writing user-formatted text files. Data written with this option cannot be read again via finput.

NoNL

This option has the same functionality as Unquoted, with the only difference that no newline character is appended to the line written by fprint.

Bin, Text

With Bin, the data is stored in MuPAD binary format. With Text, standard ASCII format is used. The default is Bin.

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"

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