The results of calculations in MATLAB are displayed on the screen after each assignment statement when the semicolon is omitted. The output format can be adjusted using the <span>format</span> command. However, more detailed control over the output can be achieved using multiple functions.
<span>disp</span> function is used to display the value of a variable according to the current format, but it does not print the variable name and “= ” first. If its argument is a string, <span>disp</span> will display that string directly. For example:
>> disp('Here is a 3-by-3 magic square'), disp(magic(3))
Here is a 3-by-3 magic square
8 1 6
3 5 7
4 9 2
More complex formatting can be accomplished using the <span>fprintf</span> function. Its syntax is <span>fprintf(format, list-of-expressions)</span>, where <span>format</span> is a string that specifies the precise output format for each expression. In the following example:
>> fprintf('%6.3f\n', pi)
3.142
<span>%</span> character indicates the start of a format specifier, requiring a field width of 6 and keeping three digits after the decimal point, while <span>\n</span> indicates a newline (if there is no newline character, subsequent output will continue on the same line). If the specified field width is insufficient, MATLAB will expand it as needed:
>> fprintf('%6.3f\n', pi^10)
93648.047
<span>f</span> format is used to display integers (using <span>%n.0f</span>) and when a fixed number of decimal places is required, such as when displaying dollars and cents (using <span>%n.2f</span>). If <span>f</span> is replaced by <span>e</span>, the number of digits after the decimal point indicates the total number of significant digits to display in scientific notation minus 1 (there will always be one digit before the decimal point):
>> fprintf('%12.3e\n', pi)
3.142e+00
When choosing field widths, remember that negative numbers take up one position for the negative sign:
>> fprintf('%5.2f\n%5.2f\n',exp(1),-exp(1))
2.72
-2.72
Adding a negative sign after the <span>%</span> character will left-align the field.
>> fprintf('%5.0f\n%5.0f\n',9,103)
9
103
>> fprintf('%-5.0f\n%-5.0f\n',9,103)
9
103
The format string can include characters that will be printed literally, as shown in the following example:
>> iter = 11; m = 5; rng(1); U = orth(randn(m)) + 1e-10;
>> fprintf('iter = %2.0f\n', iter)
iter = 11
>> fprintf('norm(U''*U-I) = %11.4e\n', norm(U'*U - eye(m)))
norm(U'*U-I) = 5.2325e-10
Note that in strings, <span>''</span> represents a single quote.
To print <span>%</span> and <span>\</span>, you can use <span>\%</span> and <span>\\</span> in the format string. Another useful format specifier is <span>g</span>, which will use the shorter result from either <span>e</span> or <span>f</span>:
>> fprintf('%g %g\n', exp(1), exp(20))
2.71828 4.85165e+08
<span>fprintf</span> supports many other specifiers and special characters, behaving similarly to the <span>printf</span> function in C (see <span>doc fprintf</span>).
If more numbers are provided than format specifiers, the format specifiers will be reused, and elements will be printed in order from the first column of the matrix to the second, and so on. This feature can avoid the use of loops. For example:
>> A = [30 40 60 70];
>> fprintf('%g miles/hour = %g kilometers/hour\n', [A; 8*A/5])
30 miles/hour = 48 kilometers/hour
40 miles/hour = 64 kilometers/hour
60 miles/hour = 96 kilometers/hour
70 miles/hour = 112 kilometers/hour
To print string variables, use the <span>s</span> format specifier. The following example uses a cell array:
>> data = {'Alan Turing', 1912, 1954};
>> fprintf('%s (%4.0f-%4.0f)\n', data{1:3})
Alan Turing (1912-1954)
<span>fprintf</span> function’s analogous function is <span>sprintf</span>, which returns the output as a string. It is very useful for generating plot labels. A simpler but less flexible alternative is <span>num2str</span>: <span>num2str(x, n)</span> converts <span>x</span> to a string with <span>n</span> significant digits, with a default of <span>n</span> being 4. For converting integers to strings, you can use <span>int2str</span>. Here are three examples, the first using the format specifier for unsigned integers <span>u</span>, the second and third using string concatenation (see Section 18.1):
>> n = 16;
>> err_msg = sprintf('Must supply a %u-by-%u matrix', n, n)
err_msg =
Must supply a 16-by-16 matrix
>> disp(['Pi is given to 6 significant figures by ' num2str(pi,6)])
Pi is given to 6 significant figures by 3.14159
>> i = 3;
>> title_str = ['Result of experiment ' int2str(i)]
title_str =
Result of experiment 3