Octave Programming Tutorial/Text and file output
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,
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
ans = that normally precedes it.
octave:3> log(10) ans = 2.3026 octave:4> disp(log(10)) 2.3026
The output of
disp depends on the
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
s is a string containing the characters shown above.
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(filename, mode)opens a file and returns an identifier for it. The
filenameargument is a string and can be the name of any new or existing file in the current directory. The
modeargument is a string that specifies whether the file is opened for
- reading (
- writing (
- appending (
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');
file_id is simply the name of a variable that we use to tell Octave which file to write.
valueto the file identified by
The output written to the file will appear exactly the same as the output from the
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
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
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.
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
printf function on line 21. It specifies what the output of
printf should look like. Essentially, the precentage 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'
%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 (
- real (
- scientific notation (
- percentage symbol (
For more types, see the Octave documentation.
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%.
%eformat outputs a value in the form , where and b is an integer;
- the variable
xis passed to
printftwice 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
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 (
octave:40> printf("%9.3f\n", pi); 3.142
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.
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.
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,
printf 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