Store data in an array of bytes.
s = bwrite(data) s = bwrite(data, precision)
bwrite(data) stores the contents of the matrix data into an array of class uint8. The second parameter is the precision, whose meaning is the same as for fread. Its default value is 'uint8'.
bwrite(12345, 'uint32;l') 1x4 uint8 array 57 48 0 0 bwrite(12345, 'uint32;b') 1x4 uint8 array 0 0 48 57
swrite, sread, fwrite, sprintf
Simple display on the standard output.
disp(obj)
disp(obj) displays the object obj. Command format may be used to control how numbers are formatted.
disp('hello') hello
Close a file.
fclose(fd) fclose('all')
fclose(fd) closes the file descriptor fd which was obtained with functions such as fopen. Then fd should not be used anymore. fclose('all') closes all the open file descriptors.
Check end-of-file status.
b = feof(fd)
feof(fd) is false if more data can be read from file descriptor fd, and true if the end of the file has been reached.
Count the number of lines and characters in a file (fopen and fclose are not available in all LME applications):
fd = fopen('data.txt'); lines = 0; characters = 0; while ~feof(fd) str = fgets(fd); lines = lines + 1; characters = characters + length(str); end fclose(fd);
Flush the input and output buffers.
fflush(fd)
fflush(fd) discards all the data in the input buffer and forces data out of the output buffer, when the device and their driver permits it. fflush can be useful to recover from errors.
Reading of a single line.
line = fgetl(fd) line = fgetl(fd, n)
A single line (of at most n characters) is read from a text file. The end of line character is discarded.
Reading of a single line.
line = fgets(fd) line = fgets(fd, n)
A single line (of at most n characters) is read from a text file. Unless the end of file is encountered before, the end of line (always a single line feed) is preserved.
Default output format.
format format short format short e format short g format long format long e format long g format bank format '+' format i format j format loose format compact
format changes the format used by command disp and for output produced with expressions which do not end with a semicolon. The following arguments are recognized:
Arguments | Meaning |
---|---|
(none) | fixed format with 0 or 4 digits, loose spacing |
short | fixed format with 0 or 4 digits |
short e | exponential format with 4 digits |
short g | general format with up to 4 digits |
long | fixed format with 0 or 15 digits |
long e | exponential format with 15 digits |
long g | general format with up to 15 digits |
bank | fixed format with 2 digits (for currencies) |
+ | '+', '-' or 'I' for nonzero, space for zero |
i | symbol i to represent the imaginary unit |
j | symbol j to represent the imaginary unit |
loose | empty lines to improve readability |
compact | no empty line |
Format for numbers, for imaginary unit symbol and for spacing is set separately. Format '+' displays compactly numeric and boolean arrays: positive numbers and complex numbers with a positive real part are displayed as +, negative numbers or complex numbers with a negative real part as -, pure imaginary nonzero numbers as I, and zeros as spaces. The default format is format short g, format j, and format compact.
Formatted output.
n = fprintf(fd,format,a,b,...) n = fprintf(format,a,b,...)
fprintf(format,a,b,...) converts its arguments to a string and writes it to the standard output. fprintf(fd,format,a,b,...) specifies the output file descriptor. See sprintf for a description of the conversion process.
fprintf('%d %.2f %.3E %g\n',1:3,pi) 1 2.00 3.000E0 3.1416 22
Same limitations as sprintf
Raw input.
(a, count) = fread(fd) (a, count) = fread(fd, size) (a, count) = fread(fd, size, precision)
fread(fd) reads signed bytes from the file descriptor fd until it reaches the end of file. It returns a column vector whose elements are signed bytes (between -128 and 127), and optionally in the second output argument the number of bytes it has read.
fread(fd,size) reads the number of bytes specified by size. If size is a scalar, that many bytes are read and result in a column vector. If size is a vector of two elements [m,n], m*n elements are read row by row and stored in an m-by-n matrix. If the end of the file is reached before the specified number of elements have been read, the number of rows is reduced without throwing an error. The optional second output argument always gives the number of elements in the result.
With a third argument, fread(fd, size, precision) reads integer words of 1, 2, or 4 bytes, or IEEE floating-point numbers of 4 bytes (single precision) or 8 bytes (double precision). The meaning of the string precision is described in the table below.
precision | meaning |
---|---|
int8 | signed 8-bit integer (-128 |
char | signed 8-bit integer (-128 |
int16 | signed 16-bit integer (-32768 |
int32 | signed 32-bit integer (-2147483648 |
int64 | signed 64-bit integer (-9.223372e18 |
uint8 | unsigned 8-bit integer (0 |
uchar | unsigned 8-bit integer (0 |
uint16 | unsigned 16-bit integer (0 |
uint32 | unsigned 32-bit integer (0 |
uint64 | unsigned 64-bit integer (0 |
single | 32-bit IEEE floating-point |
double | 64-bit IEEE floating-point |
By default, multibyte words are encoded with the least significant byte first (little endian). The characters ';b' can be appended to specify that they are encoded with the most significant byte first (big endian) (for symmetry, ';l' is accepted and ignored).
By default, the output is a double array. To get an output which has the same type as what is specified by precision, the character * can be inserted at the beginning. For instance '*uint8' reads bytes and stores them in an array of class uint8, '*int32;b' reads signed 32-bit words and stores them in an array of class int32 after performing byte swapping if necessary, and '*char' reads bytes and stores them in a character row vector (i.e. a plain string).
Reading of formatted numbers.
r = fscanf(fd, format) (r, count) = fscanf(fd, format)
A single line is read from a text file, and numbers, characters and strings are decoded according to the format string. The format string follows the same rules as sscanf.
The optional second output argument is set to the number of elements decoded successfully (may be different than the length of the first argument if decoding strings).
Read a number from a file (fopen and fclose are not available in all LME applications):
fd = fopen('test.txt', 'rt'); fscanf(fd, '%f') 2.3 fclose(fd);
Change the current read or write position in a file.
status = fseek(fd, position) status = fseek(fd, position, mode)
fseek(fd,position,mode) changes the position in an open file where the next input/output commands will read or write data. The first argument fd is the file descriptor returned by fopen or similar functions (fopen is not available in all LME applications). The second argument is the new position. The third argument mode specifies how the position is used:
b | absolute position from the beginning of the file |
c | relative position from the current position |
e | offset from the end of the file (must be
|
The default value is 'b'. Only the first character is checked, so 'beginning' is a valid alternative for 'b'. fseek returns 0 if successful or -1 if the position is outside the limits of the file contents.
Get the current read or write position in a file.
position = ftell(fd)
ftell(fd) gives the current file position associated with file descriptor fd. The file position is the offset (with respect to the beginning of the file) at which the next input function will read or the next output function will write. The offset is expressed in bytes. With text files, ftell may not always correspond to the number of characters read or written.
Raw output.
count = fwrite(fd, data) count = fwrite(fd, data, precision)
fwrite(fd, data) writes the contents of the matrix data to the output referenced by the file descriptor fd. The third parameter is the precision, whose meaning is the same as for fread. Its default value is 'uint8'.
Redirect or copy standard output or error to another file descriptor.
redirect(fd, fdTarget) redirect(fd, fdTarget, copy) redirect(fd) R = redirect(fd) redirect R = redirect
redirect(fd,fdTarget) redirects output from file descriptor fd to fdTarget. fd must be 1 for standard output or 2 for standard error. If fdTarget==fd, the normal behavior is restored.
redirect(fd,fdTarget,copy) copies output to both fd and fdTarget if copy is true, instead of redirecting it only to fdTarget. If copy is false, the result is the same as with two input arguments.
With zero or one input argument and without output argument, redirect displays the current redirection for the specified file descriptor (1 or 2) or for both of them. Note that the redirection itself may alter where the result is displayed.
With an output argument, redirect returns a 1-by-2 row vector if the file descriptor is specified, or a 2-by-2 matrix otherwise. The first column contains the target file descriptor and the second column, 1 for copy mode and 0 for pure redirection mode.
Create a new file diary.txt and copy to it both standard output and error:
fd = fopen('diary.txt', 'w'); redirect(1, fd, true); redirect(2, fd, true);
Stop copying standard output and error and close file:
redirect(1, 1); redirect(2, 2); fclose(fd);
Redirect standard error to standard output and get the redirection state:
redirect(2, 1) redirect stdout (fd=1) -> fd=1 stderr (fd=2) -> fd=1 redirect(2) stderr (fd=2) -> fd=1 R = redirect R = 1 0 1 0 R = redirect(2) R = 1 0
Formatted conversion of objects into a string.
s = sprintf(format,a,b, ...)
sprintf converts its arguments to a string. The first parameter is the format string. All the characters are copied verbatim to the output string, except for the control sequences which all begin with the character '%'. They have the form
%fn.dt
where f is zero, one or more of the following flags:
Flag | Meaning |
---|---|
- | left alignment (default is right alignment) |
+ | display of a + sign for positive numbers |
0 | zero padding instead of spaces |
# | alternate format (see below) |
space | sign replaced with space for positive numbers |
n is the optional width of the field as one or more decimal digits (default is the minimum width to display the data), d is the number of digits after the decimal separator for a number or the number of characters for a string (one or more decimal digits; by default, it is 4 for a number or the length of the string for a string), and t is a single character denoting the type of conversion. In most cases, each control sequence corresponds to an additional argument. All elements of arrays are used sequentially as if they were provided separately; strings are used as a whole. The table below gives the valid values of t.
Char. | Conversion |
---|---|
% | single % |
d | decimal number as an integer |
i | same as d |
x | hexadecimal number (for integers between 0 and 2^32-1) |
X | same as x, with uppercase digits |
o | octal number (for integers between 0 and 2^32-1) |
f | fixed number of decimals (exp. notation if abs(x)>1e18) |
F | same as f, with an uppercase E |
e | scientific notation such as 1e5 |
E | scientific notation such as 1E5 |
g | decimal or scientific notation |
G | same as g, with an uppercase E |
k | same as g, with as few characters as possible |
K | same as k, with an uppercase E |
c | character |
s | string |
The # flag forces octal numbers to begin with 0, nonzero hexadecimal numbers to begin with 0x, and floating-point numbers to always have a decimal point even if they do not have a fractional part.
Instead of decimal digits, the width n and/or the precision d can be replaced with character *; then one or two additional arguments (or elements of an array) are consumed and used as the width or precision.
sprintf('%d %.2f %.2e %.2E %.2g',pi*ones(1,5)) 3 3.14 3.14e0 3.14E0 3.14 sprintf('%.1k ', 0.001, 0.11, 111, 1000) 1e-3 0.11 111 1e3 sprintf('*%8.3f*%8.6s*%-8.6s*',pi,'abcdefgh','abcdefgh') * 3.142* abcdef*abcdef * sprintf('%c_','a':'z') a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z_ sprintf('%*.*f', 15, 7, pi) 3.1415927
Exotic formats unsupported.
Raw input from a string or an array of bytes.
(a, count) = sread(str, size, precision) (a, count) = sread(str, [], precision) (a, count) = sread(bytes, ...)
sread(str) reads data from string str or array of class uint8 or int8 the same way as fread reads data from a file.
(data, count) = sread('abc') data = 97 98 99 count = 3 (data, count) = sread('abcdef',[2,2]) data = 97 98 99 100 count = 4 (data, count) = sread('abcd',[inf,3]) data = 97 98 99 count = 3
Decoding of formatted numbers.
r = sscanf(str, format) (r, count) = scanf(str, format) (r, count, nchar) = scanf(str, format)
Numbers, characters and strings are extracted from the first argument. Exactly what is extracted is controlled by the second argument, which can contain the following elements:
Substring in format | Meaning |
---|---|
%c | single character |
%s | string |
%d | integer number in decimal |
%x | unsigned integer number in hexadecimal |
%o | unsigned integer number in octal |
%i | integer number |
%f | floating-point number |
%e | floating-point number |
%g | floating-point number |
%% | % |
other character | exact match |
%i recognizes an optional sign followed by a decimal number, an hexadecimal number prefixed with 0x or 0X, a binary number prefixed with 0b or 0B, or an octal number prefixed with 0.
The decoded elements are accumulated in the output argument, either as a column vector if the format string contains %d, %o, %x, %i, %f, %e or %g, or a string if the format string contains only %c, %s or literal values. If a star is inserted after the percent character, the value is decoded and discarded. A width (as one or more decimal characters) can be inserted before s, d, x, o, i, f, e or g; it limits the number of characters to be decoded. In the input string, spaces and tabulators are skipped before decoding %s, %d, %x, %o, %i, %f, %e or %g.
The format string is recycled as many times as necessary to decode the whole input string. The decoding is interrupted if a mismatch occurs.
The optional second output argument is set to the number of elements decoded successfully (may be different than the length of the first argument if decoding strings). The optional third output argument is set to the number of characters which were consumed in the input string.
sscanf('f(2.3)', 'f(%f)') 2.3 sscanf('12a34x778', '%d%c') 12 97 34 120 778 sscanf('abc def', '%s') abcdef sscanf('abc def', '%c') abc def sscanf('12,34','%*d,%d') 34 sscanf('0275a0ff', '%2x') 2 117 160 255
Store data in a string.
s = swrite(data) s = swrite(data, precision)
swrite(fd, data) stores the contents of the matrix data in a string. The third parameter is the precision, whose meaning is the same as for fread. Its default value is 'uint8'.
swrite(65:68) ABCD double(swrite([1,2], 'int16')) 1 0 2 0 double(swrite([1,2], 'int16;b')) 0 1 0 2