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

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

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

: format
: format options
: 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, 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.


Restore the default format state described above.


Fixed point format with 5 significant figures (default).


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.


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.


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 =


Optionally, the trailing ‘g’ can be split into a second argument.


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.


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


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


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


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


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


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.


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.


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.

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