Since Octave normally prints the value of an expression as soon as it has been evaluated, the simplest of all I/O functions is a simple expression. For example, the following expression will display the value of ‘pi’
pi -| ans = 3.1416
This works well as long as it is acceptable to have the name of the
variable (or ‘ans’) printed along with the value. To print the
value of a variable without printing its name, use the function
disp
.
The format
command offers some control over the way Octave prints
values with disp
and through the normal echoing mechanism.
(x)
¶str =
disp (x)
¶Display the value of x.
For example:
disp ("The value of pi is:"), disp (pi) -| the value of pi is: -| 3.1416
Note that the output from disp
always ends with a newline.
If an output value is requested, disp
prints nothing and returns the
formatted output in a string.
See also: fdisp.
str =
list_in_columns (arg, width, prefix)
¶Return a string containing the elements of arg listed in columns with an overall maximum width of width and optional prefix prefix.
The argument arg must be a cell array of character strings or a character array.
If width is not specified or is an empty matrix, or less than or equal to zero, the width of the terminal screen is used. Newline characters are used to break the lines in the output string. For example:
list_in_columns ({"abc", "def", "ghijkl", "mnop", "qrs", "tuv"}, 20) ⇒ abc mnop def qrs ghijkl tuv whos ans ⇒ Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== ans 1x37 37 char Total is 37 elements using 37 bytes
See also: terminal_size.
[rows, cols] =
terminal_size ()
¶([rows, cols])
¶Query or set the size of the terminal window. If called with no arguments, return a two-element row vector containing the current size of the terminal window in characters (rows and columns). If called with a two-element vector of integer values, set the terminal size and return the previous setting. Setting the size manually should not be needed when using readline for command-line editing.
See also: list_in_columns.
options
¶(options)
¶[format, formatspacing, uppercase] =
format ¶Reset or specify the format of the output produced by disp
and Octave’s
normal echoing mechanism.
This command only affects the display of numbers, not how they are stored
or computed. To change the internal representation from the default double use
one of the conversion functions such as single
, uint8
,
int64
, etc. Any format
options that change the number of
displayed significant digits will also be reflected by the
output_precision
function.
By default, Octave displays 5 significant digits in a human readable form
(option ‘short’, option ‘lowercase’, and option ‘loose’ format
for matrices). If format
is invoked without any options, or the option
‘default’ is specified, then this default format is restored.
Valid format options for floating point numbers are listed in the following table.
default
Restore the default format state described above.
short
Fixed point format with 5 significant figures (default).
long
Fixed point format with 16 significant figures.
As with the ‘short’ format, Octave will switch to an exponential ‘e’ format if it is unable to format a matrix properly using the current format.
shorte
longe
Exponential format. The number to be represented is split between a mantissa
and an exponent (power of 10). The mantissa has 5 significant digits in the
short format. In the long format, double values are displayed with 16
significant digits and single values are displayed with 8. For example,
with the ‘shorte’ format, pi
is displayed as 3.1416e+00
.
Optionally, the trailing ‘e’ can be split into a second argument.
shortg
longg
Optimally choose between fixed point and exponential format based on the
magnitude of the number. For example, with the ‘shortg’ format,
pi .^ [2; 4; 8; 16; 32]
is displayed as
ans = 9.8696 97.409 9488.5 9.0032e+07 8.1058e+15
Optionally, the trailing ‘g’ can be split into a second argument.
shorteng
longeng
Identical to ‘shorte’ or ‘longe’ but displays the value using an
engineering format, where the exponent is divisible by 3. For example, with
the ‘shorteng’ format, 10 * pi
is displayed as 31.416e+00
.
Optionally, the trailing ‘eng’ can be split into a second argument.
free
none
Print output in free format, without trying to line up columns of matrices on
the decimal point. This is a raw format equivalent to the C++ code
std::cout << variable
. In general, the result is a presentation
with 6 significant digits where unnecessary precision (such as trailing zeros
for integers) is suppressed. Complex numbers are formatted as numeric pairs
like this ‘(0.60419, 0.60709)’ instead of like this
‘0.60419 + 0.60709i’.
The following formats affect all numeric output (floating point and integer types).
"+"
"+"
"chars"
plus
plus chars
Print a ‘+’ symbol for matrix elements greater than zero, a ‘-’
symbol for elements less than zero, and a space for zero matrix elements. This
format can be useful for examining the sparsity structure of a large matrix.
For very large matrices the function spy
which plots the sparsity
pattern will be clearer.
The optional argument chars specifies a list of 3 characters to use for
printing values greater than zero, less than zero, and equal to zero. For
example, with the format "+" "+-."
, the matrix
[1, 0, -1; -1, 0, 1]
is displayed as
ans = +.- -.+
Print variable in a format appropriate for a currency (fixed format with two digits to the right of the decimal point). Only the real part of a variable is displayed, as the imaginary part makes no sense for a currency.
Print the bit representation of numbers in memory, always with the
most significant bit first. For example, pi
is printed like this:
0 10000000000 1001001000011111101101010100010001000010110100011000
where spaces have been added for clarity to show the sign bit, the 11-bit
exponent, and the 52-bit mantissa, in that order. Together they represent
pi
as an IEEE 754 double precision floating point number in the normal
form. Single precision floating point numbers are analogous.
Print the bit representation of numbers as stored in memory. For big-endian
machines, this is identical to the format bit
layout seen above.
For little-endian machines, it will print the bytes in the opposite order,
though bits within a byte will still be presented with the most significant
bit on the left.
For example, the value of pi
in this format on x86-64 is:
00011000 00101101 01000100 01010100 11111011 00100001 00001001 01000000
shown here with spaces added for clarity. Compare with the previous bit string
from format bit
to see the grouping into bytes and their ordering.
The same as format bit
above, except that bits are grouped four at a
time into hexadecimal digits for brevity. Thus pi
is represented as:
400921fb54442d18
The same as format native-bit
above, except that bits are grouped four
at a time into hexadecimal digits for brevity. Thus pi
is represented
on an x86-64 as:
182d4454fb210940
Print a rational approximation, i.e., values are approximated as the ratio of
small integers. For example, with the ‘rat’ format, pi
is
displayed as 355/113
.
The following two options affect the display of scientific and hex notations.
lowercase (default)
Use a lowercase ‘e’ for the exponent character in scientific notation and lowercase ‘a-f’ for the hex digits representing 10-15.
uppercase
Use an uppercase ‘E’ for the exponent character in scientific notation and uppercase ‘A-F’ for the hex digits representing 10-15.
The following two options affect the display of all matrices.
compact
Remove blank lines around column number labels and between matrices producing more compact output with more data per page.
loose (default)
Insert blank lines above and below column number labels and between matrices to produce a more readable output with less data per page.
If format
is called with multiple competing options, the rightmost one
is used, except for ‘default’ which will override all other options. In
case of an error the format remains unchanged.
If called with one to three output arguments, and no inputs, return the current format, format spacing, and uppercase preference. Specifying both outputs and inputs will produce an error.
See also: fixed_point_format, output_precision, split_long_rows, print_empty_dimensions, rats.