Search, 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">)
read(filename) searches for the file in
filename is concatenated
to each folder given by the environment variable
Then the file name is interpreted as an absolute path name.
Then the file name is interpreted as a relative pathname, i.e., relative to the “working folder.”
Last, the file name is concatenated to the library path.
If a file can be opened with one of these names, then the file
is read and executed with
read(filename, Encoding = "encodingValue") uses
the specified encoding for text files. For supported encodings, see Options.
If the file is in
and its name ends in “
.gz”, it will
be transparently uncompressed upon reading.
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 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.
A path separator (“
/”) is inserted
as necessary when concatenating a given path and
read(n) with a file descriptor
equivalent to the call
fread(n). When called with
a file description,
read does not automatically
open and close the file. Use
open and close the file. The
does not work with this syntax.
When you use the
read command to read a file,
the command evaluates all the statements in that file with the maximal
substitution depth defined by
LEVEL. The default value of
interactive computations is 100. See Example 3.
See the function
details about reading and executing the file's content and for a detailed
description of the options
When a file is read with
read, 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):
a := 3: b := 5: fid := fopen(TempFile, Write, Text):
to store values
b in the
write(fid, a, b):
return the name of the temporary file you created:
file := fname(fid):
After reading the file, the values of
delete a, b: read(file): a, b
open the file and read its content:
delete a, b: n := fopen(file): read(n): fclose(n): a, b
delete a, b, READPATH, n:
You can explicitly specify the folder and file
names. The following example only works on systems like UNIX.
To make it work on other operating systems, change the path names
accordingly. First, use
store values in the file “
in the “
a := 3: b := 5: write("/tmp/testfile.mb", a, b):
Now, define “
/tmp” as the search
folder and provide a path name relative to it. Note that the path
/” is inserted by
delete a, b: READPATH := "/tmp": read("testfile.mb"): a, b
read command evaluates
all the statements in a file it reads with the maximal substitution
depth defined by
LEVEL. For example, create and read a
file that specifies the value of the variable
using another variable
b. Use the
fopen command with the
to create a new file in the system's temporary folder:
fid := fopen(TempFile, Write, Text):
Write the following statements to the file:
fprint(Unquoted, fid, "a := b^2: b := 5: c := a/3: delete a, b:"):
file := fname(fid): fclose(fid)
Read the file. The
read command evaluates
the statements in the file recursively:
To supress recursive evaluations, change the maximal substitution depth to 1:
delete c: LEVEL := 1: read(file): c
Restore the default value of
LEVEL for further computations:
To specify the encoding to read data, use
Encoding option applies only to text files
that are opened using a file name and not a file descriptor. Open
a file and write the statement
"str = abcäöü" in
fprint(Unquoted, Text, Encoding="UTF-8", "read_test", "str := \"abcäöü\"")
Specify the encoding to read the file.
the correct output:
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
Suppresses output during execution of
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.