Next: , Up: Basic Input and Output


16.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
          -| pi = 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.

— Built-in Variable: ans

This variable holds the most recently computed result that was not explicitly assigned to a variable. For example, after the expression

          3^2 + 4^2
     

is evaluated, the value of ans is 25.

— Built-in Function: fdisp (fid, x)

Display the value of x on the stream fid. For example,

          fdisp (stdout, "The value of pi is:"), fdisp (stdout, pi)
          
               -| the value of pi is:
               -| 3.1416
     

Note that the output from fdisp always ends with a newline.

— Built-in Function: 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.

— Command: format options

Control the format of the output produced by disp and Octave's normal echoing mechanism. Valid options are listed in the following table.

short
Octave will try to print numbers with at least 5 significant figures within a field that is a maximum of 10 characters wide (not counting additional spacing that is added between columns of a matrix).

If Octave is unable to format a matrix so that columns line up on the decimal point and all the numbers fit within the maximum field width, it switches to an `e' format.

long
Octave will try to print numbers with at least 15 significant figures within a field that is a maximum of 20 characters wide (not counting additional spacing that is added between columns of a matrix).

As will the `short' format, Octave will switch to an `e' format if it is unable to format a matrix so that columns line up on the decimal point and all the numbers fit within the maximum field width.

long e
short e
The same as `format long' or `format short' but always display output with an `e' format. For example, with the `short e' format, pi is displayed as 3.14e+00.
long E
short E
The same as `format long e' or `format short e' but always display output with an uppercase `E' format. For example, with the `long E' format, pi is displayed as 3.14159265358979E+00.
long g
short g
Choose between normal `long' (or `short') and and `long e' (or `short e') formats based on the magnitude of the number. For example, with the `short g' format, pi .^ [2; 4; 8; 16; 32] is displayed as
               ans =
               
                     3.1416
                     9.8696
                     97.409
                     9488.5
                 9.0032e+07
                 8.1058e+15
          

long G
short G
The same as `format long g' or `format short g' but use an uppercase `E' format. For example, with the `short G' format, pi .^ [2; 4; 8; 16; 32] is displayed as
               ans =
               
                     3.1416
                     9.8696
                     97.409
                     9488.5
                 9.0032E+07
                 8.1058E+15
          

free
none
Print output in free format, without trying to line up columns of matrices on the decimal point. This also causes complex numbers to be formatted like this `(0.604194, 0.607088)' instead of like this `0.60419 + 0.60709i'.
bank
Print in a fixed format with two places to the right of the decimal point.
+
+ chars
plus
plus chars
Print a `+' symbol for nonzero matrix elements and a space for zero matrix elements. This format can be very useful for examining the structure of a large matrix.

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, [1, 0, -1; -1, 0, 1] is displayed as

               ans =
               
               +.-
               -.+
          
native-hex
Print the hexadecimal representation 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 hex format is 400921fb54442d18. This format only works for numeric values.
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 bit format on a workstation which stores 8 byte real values in IEEE format with the least significant byte first. This format only works for numeric types.

bit
The same as native-bit, but always print the most significant bits first.
compact
Remove extra blank space around column number labels.
loose
Insert blank lines above and below column number labels (this is the default).

By default, Octave will try to print numbers with at least 5 significant figures within a field that is a maximum of 10 characters wide.

If Octave is unable to format a matrix so that columns line up on the decimal point and all the numbers fit within the maximum field width, it switches to an `e' format.

If format is invoked without any options, the default format state is restored.

— Built-in Variable: print_answer_id_name

If the value of print_answer_id_name is nonzero, variable names are printed along with the result. Otherwise, only the result values are printed. The default value is 1.