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 | TempFile, <
Read | Write | Append>, <
Bin | Text | Raw>, <
Encoding = "encodingValue">)
fopen(filename, format) opens an existing
file for reading in the specified format. An error is raised if no
file with the specified name is found or the format of the file does
not coincide with the specified
format. If the
file is in
gzip-compressed format and its name
ends in “
.gz”, it will be transparently
uncompressed upon reading.
fopen(filename) opens an existing file for
reading. The file must hold data in text or MuPAD® binary format
fopen automatically identifies
the file format in this case. The file must not be used as raw file.
fopen(filename, mode, format) opens the
file for writing in the specified format if the mode is given as
If no file with the specified name exists, a new file is created.
If the filename ends in “
data written to the file will be transparently compressed in
fopen(TempFile, format) creates and opens
a temporary file for writing in the specified format. The option
not allowed in this case. If no format is given,
query the actual name and location of the temporary file. Cf. Example 3.
fopen(..., Encoding = "encodingValue") uses
the specified encoding. For supported encodings, see Options. You can use this option with
the previously specified syntaxes for text files.
In write mode (using one of the options
the environment variable
WRITEPATH is considered if no temporary
file is created. If it has a value, a new file is created (or an existing
file is searched for) in the corresponding folder. Otherwise, it is
created/searched for in 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.
In read mode,
fopen does not search for files
in the folders given by the library path.
A temporary file is created in a special folder. This folder and the name of the file are system dependent.
Also absolute path names are processed by
A file opened by
fopen should be closed by
fclose after use. This
holds also for temporary files.
fopen accepts its arguments in any order,
not only in the order used above.
The function is sensitive to the environment variable
creating files that are not temporary (temporary files are created
WRITEPATH has a value,
in write mode (using the options
the file is created in the corresponding folder. Otherwise, the file
is created in the “working folder.” A temporary file
is created in a special folder.
a new file if no file under the given name exists.
Open the file
test for writing.
With the option
Write, it is not necessary that
test exists. By default, the file is opened
as a binary file:
fid := fopen("test", Write):
Write a string to the file and close it:
fprint(fid, "a string"): fclose(fid):
Append another string to the file:
fid := fopen("test", Append): fprint(fid, "another string"): fclose(fid):
The binary file cannot be opened as a text file for appending data:
fid := fopen("test", Append, Text)
However, it may be opened as a text file with the option
The existing binary file is overwritten with a text file:
fid := fopen("test", Write, Text): fclose(fid): delete fid:
fopen fails to open non-existing
files for reading. Here, assume the file “
does not exist:
n := fopen("xyz")
Assume the file “
in Example 1 exists.
It can be opened for reading successfully:
n := fopen("test")
fclose(n): delete n:
Open a temporary file, write 10 binary data
bytes into it and close it.
used to query the name of the file:
fd := fopen(TempFile, Raw): writebytes(fd, [i $ i=1..10]): fn := fname(fd): fclose(fd): fn
Re-open the file and read the data:
fd := fopen(fn, Read, Raw): readbytes(fd);
fclose(fd): delete fd, fn:
To specify the encoding to read and write
applies only to text files that are opened using a file name and not
a file descriptor. Create a temporary file and write the string
fid := fopen(TempFile, Text, Write, Encoding="UTF-8"): file := fname(fid): fprint(Unquoted, fid, "abcäöü"): fclose(fid):
ftextinput to read the data with the
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:
fid := fopen(TempFile, Text, Write): file := fname(fid): fprint(Unquoted, fid, "abcäöü"): fclose(fid): ftextinput(file)
The name of a file: a character string or the flag
If the mode is
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.
a positive integer: the file descriptor.
FAIL is returned if
the file cannot be opened.