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
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 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 4 decimal digits, loose spacing |
short | fixed format with 4 decimal digits |
short e | exponential format with 4 decimal digits |
short g | general format with up to 4 decimal digits |
long | fixed format with 15 decimal digits |
long e | exponential format with 15 decimal digits |
long g | general format with up to 15 decimal digits |
loose | empty lines to improve readability |
compact | no empty line |
Format for numbers and for spacing can be set separately. The default format is format short g 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.
With a third argument, fread(fd, size, precision) reads integer words of 1, 2, or 4 bytes. The meaning of the string precision is described in the table below.
precision | meaning |
---|---|
int8 | signed 8-bit integer (byte, between -128 and 127) |
char | same as int8 |
int16 | signed 16-bit integer (-32768 |
int32 | signed 32-bit integer (-2147483648 |
uint8 | unsigned 8-bit integer (0 |
uchar | same as uint8 |
uint16 | unsigned 16-bit integer (0 |
uint32 | unsigned 32-bit integer (0 |
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).
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.
fd = fopen('test.txt', 'rt'); sscanf(fd, '%f') 2.3 fclose(fd);
Raw output.
count = fwrite(fd, data) count = fwrite(fd, data, precision)
fwrite(fd, data) writes the content 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'.
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
%-+n.dt
where - (optional) indicates left alignment of the field (default is right alignment), + (optional) forces the display of a + sign 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 |
x | hexadecimal number (for integers between 0 and 2^32-1) |
X | same as x, with uppercase digits |
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 |
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.
(a, count) = sread(str, size, precision) (a, count) = sread(str, [], precision)
sread(str) reads data from string str 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)
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 |
%x | unsigned integer number in hexadecimal |
%f | floating-point number |
%e | floating-point number |
%g | floating-point number |
%% | % |
other character | exact match |
The decoded elements are accumulated in the output argument, either as a column vector if the format string contains %d, %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, 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, %f, %e or %g. 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 format string is recycled as many times as necessary to decode the whole input string. The decoding is interrupted if a mismatch occurs.
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 content 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