# The disp function

The disp function displays the value of a variable (scalar, vector, matrix, string, etc.) in the same way as simply typing the name of the variable does. For example,
The name of the variable is, however, not displayed. You can also display the result of a computation without the ans = that normally precedes it.
The output of disp depends on the format command.
The displayed value can be printed to the screen, saved in a string or saved to a file using fdisp.
Note that s is a string containing the characters shown above.

## File output

The fdisp function can be used to save values to a file. Before we can do this, we have to open a file. This is done using the fopen command.
* fopen(filename, mode) opens a file and returns an identifier for it. The filename argument is a string and can be the name of any new or existing file in the current directory. The mode argument is a string that specifies whether the file is opened for
* reading (r),
* writing (w), or
* appending (a).
When a file is opened for writing, its contents are erased and replaced with new data. To keep the existing data in a file and add to the end thereof, use the append mode.
Here, file_id is simply the name of a variable that we use to tell Octave which file to write.
* fdisp(file_id, value) writes value to the file identified by file_id.
The output written to the file will appear exactly the same as the output from the disp command.
It is important to close a file after all data has been written to it. Closing the file tells Octave to finalise any output that might still be pending and frees up the file so that it can be opened by other users or programmes.
* fclose(file_id) closes the file identified by file_id.

# The printf function

The printf function is considerably more powerful than disp and, consequently, a bit more complicated to use. With printf, you can define exactly what the output of a value should look like. This includes specifying
* the number of significant digits to display;
* the format of the number (integer, real, scientific, etc.);
* other output to display before or after the value.
Since there are so many different ways to format output using printf, we will discuss only the basics here using examples. For more complete information, type
in Octave and page through the help using the spacebar key.

## Outputting to screen, string or file

The printf function displays its output on the screen. Use the sprintf to return the result in a string and fprintf to write to a file. Note that the fprintf requires one additional parameter to specify the file identifier.
* printf(format, value, ...)
* sprintf(format, value, ...)
* fprintf(fid, format, value, ...)
Note that these functions can output more than one value at the same time--more on this in the next section.

## The format string

Let's look at an example.
The important part is the first argument to the printf function on line 21. It specifies what the output of printf should look like. Essentially, the percentage sign (%) indicates that a value should be placed at its position in the format string. In the format string
the %i is replaced with an integer, the %f with a real (f is for floating point) value, and the %s with a string. The values of the integer, real and string are given as arguments to printf after the format string. Note that x in the example above equals 10.1, but the value displayed is 10 since we specified that printf should display an integer. Finally, the \n at the end of the string tells Octave to move to a new line.
The next example demonstrates the following types:
* integer (%i),
* real (%f),
* scientific notation (%e),
* percentage symbol (%%).
For more types, see the Octave documentation.
Note that
* the %e format outputs a value in the form $a\times 10^{b}$, where $1\leq a<10$ and b is an integer;
* the variable x is passed to printf twice on line 23 since we want to output it twice (in different formats);
* the double percentage (%%) on line 24 outputs a single percentage symbol.
We can customize the output of values even further by specifying
* the width of the output, and
* the precision of the output.
The width allows you to right align numbers and is specified between the percentage and the format specifier. For example,
Note that in the printf, each format specifier contains an integer (9). This tells printf to use 9 columns to output the integer, real and string.
The effect of the precision parameter depends on the type of output. It's most obvious use is to specify the number of digits to display after the decimal point of a real number. The precision is specified after the width and is preceded by a dot (.).
This displays π using 9 columns and to 3 digits after the decimal point. Note that the number is rounded. For other uses of the precision parameter (e.g. for integers and strings), see the Octave help.

## Outputting matrices

When the value passed to the printf function is a matrix or vector, all of the values in it are printed. If there is only one format specifier in the format string, it is used for each value in the matrix.
Note that the values are read from the matrix in column-major order, i.e. all the values from the first column are displayed first, then the second column, etc..
If there is more than one format specifier in the format string, printf cycles through them.
The values are still read in column-major order.
Return to the Octave Programming Tutorial index