FUNCTION REFERENCE

fopen

Open a file and return a file identifier.

Open a file and return a file identifier.

Syntax

fid = fopen(filename)

Syntax

fid = fopen(filename, permission)

Syntax

[fid, msg] = fopen(filename, permission)

Syntax

fid = fopen(filename, permission, machinefmt, encoding)

Syntax

ids = fopen('all')

Syntax

[name, permission] = fopen(fid)

permission is 'r' (default), 'w', 'a', 'r+', 'w+' or 'a+', optionally with b or t.

With t, writes translate LF to CR LF and reads translate CR LF to LF on all

platforms, matching Windows MATLAB. Without t, bytes are unchanged. File positions

remain byte offsets in the physical file, including CR bytes.

fid is 3 for the first open file, then 4, 5, ... and reuses the lowest free number.

When the file cannot be opened fid is -1 and msg explains why.

machinefmt 'n', 'l', 'a', 'ieee-le' or 'ieee-le.l64' (little endian, the default)

or 'b', 's', 'ieee-be' or 'ieee-be.l64' sets the byte order of later fread and

fwrite calls on that file; any other machinefmt is "Invalid machine format.".

encoding is 'UTF-8' (default) or 'ISO-8859-1' ('latin1'), one byte per character.

Relative names are taken from the current folder. At most 64 files are open at once.

Example

[fid, msg] = fopen('nothing.txt')   % fid = -1, msg = 'No such file or directory'

Differences from MATLAB: files over 8 MiB, devices and other special files,

network paths and URLs are refused; encodings other than UTF-8 and ISO-8859-1 are

not supported yet; fopen(fid) returns 'native' (or 'ieee-be') as machine format

and 'UTF-8' as default encoding, where MATLAB reports the system encoding

(for example 'US-ASCII').

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.