Octave Programming Tutorial/Text and file output

= The  function = The  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, octave:1> x = [1, 2, 3] x = 1 2  3 octave:2> disp(x) 1 2  3 The name of the variable is, however, not displayed. You can also display the result of a computation without the  that normally precedes it. octave:3> log(10) ans = 2.3026 octave:4> disp(log(10)) 2.3026

The output of  depends on the   command. octave:5> format long octave:6> disp(log(10)) 2.30258509299405

The displayed value can be printed to the screen, saved in a string or saved to a file using. octave:7> s = disp(log(10)) s = 2.30258509299405 Note that  is a string containing the characters shown above.

File output
The  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  command. 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. octave:10> file_id = fopen('mydata.txt', 'w'); Here,  is simply the name of a variable that we use to tell Octave which file to write. The output written to the file will appear exactly the same as the output from the  command.
 * opens a file and returns an identifier for it. The  argument is a string and can be the name of any new or existing file in the current directory. The   argument is a string that specifies whether the file is opened for
 * reading ,
 * writing, or
 * appending.
 * writes  to the file identified by.

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.
 * closes the file identified by.

= The  function = The  function is considerably more powerful than   and, consequently, a bit more complicated to use. With, 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, we will discuss only the basics here using examples. For more complete information, type doc printf in Octave and page through the help using the spacebar key.

Outputting to screen, string or file
The  function displays its output on the screen. Use the  to return the result in a string and   to write to a file. Note that the  requires one additional parameter to specify the file identifier.

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. octave:18> x = 10.1; octave:19> y = 5.5; octave:20> z = 'test'; octave:21> printf('An integer: %i. A real: %f. This is a %s.\n', x, y, z); An integer: 10. A real: 5.500000. This is a test. The important part is the first argument to the  function on line 21. It specifies what the output of  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 'An integer: %i. A real: %f. This is a %s.\n' the  is replaced with an integer, the   with a real (  is for floating point) value, and the   with a string. The values of the integer, real and string are given as arguments to  after the format string. Note that  in the example above equals 10.1, but the value displayed is 10 since we specified that   should display an integer. Finally, the  at the end of the string tells Octave to move to a new line.

The next example demonstrates the following types: For more types, see the Octave documentation.
 * integer ,
 * real ,
 * scientific notation ,
 * percentage symbol.

octave:22> x = 10.34; octave:23> printf("x is a real number: %f (%e in scientific notation).\n", x, x); x is a real number: 10.340000 (1.034000e+01 in scientific notation). octave:24> printf("Write percentages as %i%%.\n", x); Write percentages as 10%.

Note that
 * the  format outputs a value in the form $$a\times10^b$$, where $$1\le a<10$$ and b is an integer;
 * the variable  is passed to   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, octave:36> x = 10; octave:37> y = pi; octave:38> z = 'test'; octave:39> printf("%9i\n%9f\n%9s\n", x, y, z); 10 3.141593      test Note that in the, each format specifier contains an integer (9). This tells  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. octave:40> printf("%9.3f\n", pi); 3.142 This displays &pi; 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  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. octave:51> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; octave:52> printf("%i\n", A) 1 4 7 2 5 8 3 6 9 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,  cycles through them. octave:57> printf("[%i, %.1f, %.2e]\n", A) [1, 4.0, 7.00e+00] [2, 5.0, 8.00e+00] [3, 6.0, 9.00e+00] The values are still read in column-major order.

Return to the Octave Programming Tutorial index