Read and execute 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.


fread(filename | n, <Quiet>, <Plain>, <Encoding = "encodingValue">)


fread(file) reads and executes a MuPAD® file.

fread(filename) reads the file and evaluates each MuPAD statement in the file. If the filename ends in “.gz” and the file is in gzip-compressed format, it will be transparently uncompressed upon reading. fread automatically opens the file, performs the read operation, and closes the file.

fread(..., Encoding = "encodingValue") uses the specified encoding. For supported encodings, see Options. You can use this option with the previously specified syntaxes.

fread is similar to read. The only difference is that fread does not search for files in the folders given by READPATH and by the library path; fread only searches for the file 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 fread.

fread can read MuPAD binary files (created via fprint or write) as well as ASCII text files. fread recognizes the format of the file automatically.

Instead of a file name, a file descriptor of a file opened via fopen can also be used. See Example 3. When a file descriptor is used, fread does not automatically open and close the file. fclose must be used to close the file.

When a file is read with fread, the variable FILEPATH contains the path of the file.


Example 1

Create a new file in the system's temporary folder. The name of the temporary folder varies for different platforms. The fopen command with the TempFile option creates a file in any system's temporary folder (if such folder exists):

fid := fopen(TempFile, Write, Text): 
fprint(Unquoted, fid, "a := 3; b := 5; a + b;"):

Use fname to return the name of the temporary file you created. Use fclose to close the file:

file := fname(fid):

When reading the file, MuPAD executes the statements. Each produces a print output. The second 8 below is the return value of fread:

delete a, b:

Now, the variables a and b have the values assigned inside the file:

a, b

With the option Quiet, only the return value of fread is printed:

delete a, b:
fread(file, Quiet)

delete a, b:

Example 2

The next example demonstrates the option Plain. First, an appropriate input file is created:

fid := fopen(TempFile, Write, Text): 
fprint(Unquoted, fid,
       "f := proc(x) begin x^2 end_proc:",
       "a := f(3): b := f(4):"):
file := fname(fid):

Define an alias for f:

alias(f = "some text"):

An error occurs if you try to read the file without the option Plain. In the parser context of the MuPAD session, the alias replaces f by the corresponding string in the assignment f := .... However, strings cannot be assigned a value:

Error: Invalid left-hand side. [_assign]
  Reading File: /tmp/mupad.351omQ

With the option Plain, no such error arises: the alias for f is ignored by fread:

fread(file, Plain):
a, b

delete f, a, b:

Example 3

You use write to save the value of the identifier a in a temporary file:

a := PI + 1:
fid := fopen(TempFile, Write, Text): 
file := fname(fid):
write(file, a):
delete a:

This file is opened for reading with fopen:

n := fopen(file):

The file descriptor returned by fopen can be passed to fread. Reading the file restores the value of a:


delete a:

Example 4

To specify the encoding for reading data, use Encoding. The Encoding option applies only to text files that are opened using a file name and not a file descriptor. Open a temporary file and write the statement str := "abcäöü" in the encoding "UTF-8":

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

Specify the encoding to read the file. fread executes the statement:

fread("fread_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:




The name of a file: a character string


A file descriptor provided by fopen: a positive integer



Makes fread use its own parser context

With this option, the file is read in a new parser context. This means that the history, as returned by the command history, is not modified by the statements in the file. Further, abbreviations set outside the file via alias or user-defined operators are ignored during the execution of the file. This option is useful for reading initialization files in a clean environment.


Suppresses output during execution of fread

With this option, output is suppressed while reading and executing the file. However, warnings and error messages as well as the output of print commands are still visible.


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.

Return Values

Return value of the last statement of the file.