Input/Output Functions

disp

Simple display on the standard output.

Syntax

disp(obj)

Description

disp(obj) displays the object obj. Command format may be used to control how numbers are formatted.

Example

disp('hello')
hello

See also

format, fprintf

fgetl

Reading of a single line.

Syntax

line = fgetl(fd)
line = fgetl(fd, n)

Description

A single line (of at most n characters) is read from a text file. The end of line character is discarded.

See also

fgets, fscanf

fgets

Reading of a single line.

Syntax

line = fgets(fd)
line = fgets(fd, n)

Description

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.

See also

fgetl, fscanf

format

Default output format.

Syntax

format
format short
format short e
format short g
format long
format long e
format long g
format loose
format compact

Description

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:

ArgumentsMeaning
(none)fixed format with 4 decimal digits, loose spacing
shortfixed format with 4 decimal digits
short eexponential format with 4 decimal digits
short ggeneral format with up to 4 decimal digits
longfixed format with 15 decimal digits
long eexponential format with 15 decimal digits
long ggeneral format with up to 15 decimal digits
looseempty lines to improve readability
compactno empty line

Format for numbers and for spacing can be set separately. The default format is format short g and format compact.

See also

disp, fprintf

fprintf

Formatted output.

Syntax

n = fprintf(fd,format,a,b,...)
n = fprintf(format,a,b,...)

Description

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.

Example

fprintf('%d %.2f %.3E %g\n',1:3,pi)
1 2.00 3.000E0 3.1416
  22

Caveat

Same limitations as sprintf

See also

sprintf, fwrite

fread

Raw input.

Syntax

(a, count) = fread(fd)
(a, count) = fread(fd, size)
(a, count) = fread(fd, size, precision)

Description

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.

precisionmeaning
int8signed 8-bit integer (byte, between -128 and 127)
charsame as int8
int16signed 16-bit integer (-32768 <= x <= 32767)
int32signed 32-bit integer (-2147483648 <= x <= 2147483647)
uint8unsigned 8-bit integer (0 <= x <= 255)
ucharsame as uint8
uint16unsigned 16-bit integer (0 <= x <= 65535)
uint32unsigned 32-bit integer (0 <= x <= 4294967295)

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).

See also

fwrite, sread

fscanf

Reading of formatted numbers.

Syntax

r = fscanf(fd, format)
(r, count) = fscanf(fd, format)

Description

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.

Example

fd = fopen('test.txt', 'rt');
sscanf(fd, '%f')
  2.3
fclose(fd);

See also

sscanf

fwrite

Raw output.

Syntax

count = fwrite(fd, data)
count = fwrite(fd, data, precision)

Description

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'.

See also

fread, swrite

sprintf

Formatted conversion of objects into a string.

Syntax

s = sprintf(format,a,b, ...)

Description

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 %
ddecimal number as an integer
xhexadecimal number (for integers between 0 and 2^32-1)
Xsame as x, with uppercase digits
ffixed number of decimals (exp. notation if abs(x)>1e18)
Fsame as f, with an uppercase E
escientific notation such as 1e5
Escientific notation such as 1E5
gdecimal or scientific notation
Gsame as g, with an uppercase E
ksame as g, with as few characters as possible
Ksame as k, with an uppercase E
ccharacter
sstring

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.

Examples

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

Caveat

Exotic formats unsupported.

See also

fprintf, sscanf, swrite

sread

Raw input from a string.

Syntax

(a, count) = sread(str, size, precision)
(a, count) = sread(str, [], precision)

Description

sread(str) reads data from string str the same way as fread reads data from a file.

Examples

(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

See also

swrite, fread

sscanf

Decoding of formatted numbers.

Syntax

r = sscanf(str, format)
(r, count) = scanf(str, format)

Description

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 formatMeaning
%csingle character
%sstring
%dinteger number
%xunsigned integer number in hexadecimal
%ffloating-point number
%efloating-point number
%gfloating-point number
%%%
other characterexact 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.

Examples

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

See also

sprintf

swrite

Store data in a string.

Syntax

s = swrite(data)
s = swrite(data, precision)

Description

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'.

Examples

swrite(65:68)
  ABCD
double(swrite([1,2], 'int16'))
  1 0 2 0
double(swrite([1,2], 'int16;b'))
  0 1 0 2

See also

fwrite, sprintf


Copyright 1998-2001, Calerga.

All rights reserved.