Skip to main content
Logo image

Section 5.6 Formatted Output

Recall the general syntax of the fprintf() command:
>> fprintf(fid, format string, A1,...An)
Here, fid is optional and is used for output to a file instead of the screen. More on this topic below.
The format string contains literal text, escape sequences and format specifiers.
Escape sequences include
  • \n: newline and carriage return
  • \b: backspace
  • \t: horizontal tab
Format specifiers follow this syntax:
%[flag][width][.precision]type
Note: the brackets [] are optional, i.e. %d without surrounding brackets is acceptable.
Flag
  • -: left-justified
  • +: signed values displayed with a + sign
  • 0: pad with leading zeros instead of spaces
Width
  • Minimum field width (will use wider if number won’t fit)
Precision
  • for %e, %f number of digits to right of decimal place
  • for %g maximum number of significant figures
  • for %i, %d minimum number of digits --- pad rest with blanks or zeros
Type
  • e E: lower/upper case exponential notation (1.20500e_001 or 1.20500E+001)
  • f: fixed point notation (12.05000)
  • g G: shorter of e or f (E or f)
  • i d: integer
The fprintf() command is vectorized in MATLAB, so formatting commands are applied to all elements of arrays A1,...An in column order. Let’s take a look at an example:
>> x = 0:6;        % row vector of integers 0 to 6
>> y = sqrt(x);    % row vector of square root of integers
>> T = [x; y];     % 2x7 matrix; first row is x, second row is y
>> fprintf('number sqrt\n')
>> fprintf('  %2d  %5.2f\n', T)
number  sqrt
   0    0.00       % for a matrix, fprintf() operates on each
   1    1.00       % column, i.e. each column of matrix T.
   2    1.41
   3    1.73
   4    2.00
   5    2.24
   6    2.45
This output may at first be surprising. Let’s take a closer look. Here is what the matrix T looks like by itself:
>> T
T =
    0    1.0000    2.0000    3.0000    4.0000    5.0000    6.0000
    0    1.0000    1.4142    1.7321    2.0000    2.2361    2.4495
When processing the fprintf() command, MATLAB reads through this matrix column by column and repeatedly applies the format specifiers until no elements are left. So the first %2d is applied to integer 0 (first element of the first column of T), the following %5.2f is applied to the element 0 in column 1, row 2 of T, then a new line is executed and since there are elements left in T, we start over again but this time with the second column of T. This process continues until the last column of T has been processed.
One might think at first sight that the following would produce a similar result:
>> fprintf('  %2d  %5.2f\n', x , y)
But take a look at what happens:
 0   1.00
 2   3.00
 4   5.00
 6   0.00
 1   1.41
1.732051e+00   2.00
2.236068e+00   2.45
Since both x and y are row vectors, MATLAB uses their entries column-by-column, which means first all of the entries of x (you can see them in the above output as 0, 1.00, 2, 3.00, 4, 5.00, 6) and then the entries of y. This is obviously not what we had intended.

Activity 5.3.

Modify your script from last class so that it displays the following chart:
                     Temperature (F)
Speed (mi/h)   40  30  20  10   0  -10  -20  -30  -40
    10         34  21   9  -4 -16  -28  -41  -53  -66 
    20         30  17   4  -9 ...
    30         28  15   ...
    40          .
    50          .
    60          .
Here is our script from last class:
T = input('Enter a temperature (F): ');
v = input('Enter the wind speed (mph): ');
TWC = 35.74 + .6215*T - 35.75*v^(.16) + .4275*T*v^(.16);

disp('The wind chill temperature is ...');
disp(TWC);
Please paste your code into the submission box.
Rather than writing to the screen we can also use the fprintf() command to save output to a file, but first we must open the file:
The process of writing to a file is just like with C:
  1. file_id = fopen('filename','permission');
    
  2. fprintf(file_id, 'formatting string', vars);
    
  3. fclose(file_id);
    
The file permission options are:
  • 'w': open for write (writes over exiting file or creates new)
  • 'a': append to (appends to end of existing file or creates new)
  • 'r': open for reading