8.7. Printing output#

We can use the fprintf() function to print a string to the command window.

fprintf(string)

Lets print a short header that tells someone what our program does. Edit your program so that the following appears at the top of the file

fprintf("\nDegrees to radians conversion\n-----------------------------")

Running your program you should see the following printed to the console.

Degrees to radians conversion
-----------------------------
0.785398175

Note that the \n character created a newline in the output.


8.7.1. Printing text and numbers#

To print text and numbers in the same string we use

fprintf("some text %x.x", variable)

where %x.x is a formatting operator that controls how the variable is printed. To demonstrate this edit your program so that it looks like the following.

fprintf("\nDegrees to radians conversion\n-----------------------------")

angle_in_degrees = 45;
angle_in_radians = angle_in_degrees * pi / 180;

fprintf("\nangle in degrees = %6.3f\n", angle_in_degrees)
fprintf("angle in radians = %6.3f\n", angle_in_radians)

Running your program you should see the following printed to the console.

Degrees to radians conversion
-----------------------------
angle in degrees = 45.000
angle in radians =  0.785

Notice that the angles have been printed using 3 decimal places. This is because we have used the formatting operator %6.3f which tells MATLAB to print a float value that uses a total of 6 character spaces (including the decimal point) with 3 significant figures following the decimal point.

The different types of formatting operators are shown in Table 8.3.

Table 8.3 Format specifiers#

Data type

MATLAB code

Output

integer

fprintf("%3d \n", 2)

##2 (two blank spaces followed by 2)

float

fprintf("%10.3f \n", 1/3)

#####0.333

Scientific notation

fprintf("%10.2e \n", 123456)

##1.23e+05 (equivalent to \(1.23\times 10^{5}\))

String

fprintf("%10s world \n", "hello")

#####hello world

If the number to the left of the decimal point is zero, MATLAB will use the smallest number of spaces required to print the number, e.g., fprintf("%0.2f \n", 1.23456) will print 1.23.


8.7.2. The newline character#

The newline character, \n, is used to instruct MATLAB to print the rest of the string on new line. For example

fprintf("This text \nis printed\n\non multiple lines \n\n\nusing a single print command.\n")

will print

This text 
is printed

on multiple lines 


using a single print command.

8.7.3. Exercise#

Exercise 8.5

Write a program that determines the number of years, weeks, days, minutes and seconds that are in a given number of seconds. Your program should print out the result as a single sentence that mixes text and numbers. For example, if the given number of seconds is 1 million, then your program should print

There are 0 years, 1 weeks, 4 days, 13 hours, 46 minutes and 40 seconds in 1000000 seconds.

Use your program to print the number of years, weeks, days, hours and minutes in 1 billion seconds.