Next: , Up: Basic Input and Output   [Contents][Index]


14.1.1 Terminal Output

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.

disp (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.

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.

terminal_size ()

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.

format
format 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, but 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.

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, this default format is restored.

Valid format options for floating point numbers are listed in the following table.

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.

short e
long e

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 ‘short e’ format, pi is displayed as 3.1416e+00.

short g
long g

Optimally choose between fixed point and exponential format based on the magnitude of the number. For example, with the ‘short g’ format, pi .^ [2; 4; 8; 16; 32] is displayed as

ans =

      9.8696
      97.409
      9488.5
  9.0032e+07
  8.1058e+15
short eng
long eng

Identical to ‘short e’ or ‘long e’ but displays the value using an engineering format, where the exponent is divisible by 3. For example, with the ‘short eng’ format, 10 * pi is displayed as 31.416e+00.

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 =

+.-
-.+
bank

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.

native-hex

Print the hexadecimal representation of numbers as they are stored in memory. For example, on a workstation which stores 8 byte real values in IEEE format with the least significant byte first, the value of pi when printed in native-hex format is 400921fb54442d18.

hex

The same as native-hex, but always print the most significant byte first.

native-bit

Print the bit representation of numbers as stored in memory. For example, the value of pi is

01000000000010010010000111111011
01010100010001000010110100011000

(shown here in two 32 bit sections for typesetting purposes) when printed in native-bit format on a workstation which stores 8 byte real values in IEEE format with the least significant byte first.

bit

The same as native-bit, but always print the most significant bits first.

rat

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

See also: fixed_point_format, output_precision, split_long_rows, print_empty_dimensions, rats.


Next: , Up: Basic Input and Output   [Contents][Index]