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.
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 “
and the file is in
gzip-compressed format, it will
be transparently uncompressed upon reading.
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
not search for files in the folders given by
READPATH and by the
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
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.
be used to close the file.
When a file is read with
fread, the variable
the path of the file.
Create a new file in the system's temporary
folder. The name of the temporary folder varies for different platforms.
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;"):
file := fname(fid): fclose(fid)
When reading the file, MuPAD executes the statements. Each
produces a print output. The second
8 below is
the return value of
delete a, b: fread(file);
Now, the variables
the values assigned inside the file:
With the option
Quiet, only the return value
fread is printed:
delete a, b: fread(file, Quiet)
delete a, b:
The next example demonstrates the option
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): fclose(fid)
alias(f = "some text"):
An error occurs if you try to read the file without the option
In the parser context of the MuPAD session, the alias replaces
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(file, Plain): a, b
unalias(f): delete f, a, b:
write to save the value of the identifier
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
n := fopen(file):
The file descriptor returned by
fopen can be passed to
Reading the file restores the value of
fclose(n): delete a:
To specify the encoding for reading data,
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
:= "abcäöü" in the encoding
fprint(Unquoted, Text, Encoding="UTF-8", "fread_test", "str := \"abcäöü\"")
Specify the encoding to read the file.
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
With this option, the file is read in a new parser context.
This means that the history, as returned by the command
Suppresses output during execution of
With this option, output is suppressed while reading and executing
the file. However, warnings and error messages as well as the output
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 value of the last statement of the file.