In this lesson, we will cover how to display values or print them into sentences. This covers:
1. Disp
2. Fprintf
video coming soon!
This is useful when you want the output of a code to look neater in a table, or in specific sentences. By default, if you don’t use semicolons at the end of a line of code, MATLAB will display all outputs of code in the Command Window, but what if you don’t want it to display the answer as it is in the code? For example, rather than showing x = 5 in the Command Window, you wanted to display “The value of x is 5” ? So let’s cover the different ways we can display the output values. This can be done using disp or fprintf.
Disp
Disp is very straightforward. All you do is type disp, followed by round brackets ( ) and single quotes ‘ ‘ around the words you want to display. Here I wanted to display the words “The value of x is 5 and y is 2.”
This is what the Command Window displays when the code is run:
If you want to display a variable without words (which MATLAB does display by default anyways, but you can get it to display at a specific point in your code if you’ve defined it elsewhere), then you just need to display the variable within the brackets. Here, I have defined variables x and y by setting them equal to numbers. Then I added them and multiplied them together in the next two lines. In the last line, I wrote disp(a) so that the value of a (which was x+y) is shown as the output in the Command Window, because I have put semicolons at the end of all the other lines so none of the values display until the end of the code when I choose which ones to show by using that disp statement.
This is what we get in the Command Window. The value of variable a is displayed, which was x+y from the code above, doing 5+2 because x=5 and y=2, it gives 7.
The issue with disp is that you cannot put variables into sentences with this, which is why we have fprintf, so let’s figure out how to use it below.
Fprintf
With disp, say we have the user entering two numbers (x and y) which get added together. In this case, you don’t know what x and y equal until the user enters them, so you can’t make a sentence in the output saying something like “the value of x is 5 and the value of y is 2.” With fprintf, we are able to do this and more. Here are some examples with using fprintf.
Ex. 1: Making a Table Using fprintf
One of the ways to use fprintf is to create a table. Say we have a vector x = [1 2 3 4 5 6 7 8 9 10]. Then we plug these values into the equation y = 2*x, and we will get an output vector of y with each value of x multiplied by 2. The output will look like the image below. But what if we want to make a table instead of displaying the values like this?:
This is where fprintf comes in. We will use two lines using this. One line will be to create the table headers (x and y) and the other line will be to format the numbers within the table. I have shown the answer below, but let’s go through the two fprintf lines:
Explanation
(1) fprintf(‘ x y\n’)
This line formats the heading of the table columns. I want the first column to be x, and the second column to be y. Using single quotes within the brackets, type in the wording of the columns and it will be displayed exactly like that, right down to the number of spaces you want to keep between them. This is why you see empty space between the x and y – if I didn’t have those spaces, then the column headers would be very close together like this, which is not what we want:
(2) fprintf(‘%2.0f %9.6f\n’,x,y)
This line is here to format the columns of the table. Within the fprintf brackets, you will need single quotes for the first part, followed by the variable (or variables) you want to print out (like I have x,y at the end of brackets, not in single quotes).
The first part of this line within brackets requires single quotes with a percent sign (%) followed by a number (which isn’t mandatory, if you don’t input a number then MATLAB will default to a specific format) and a letter. For example, like I have %2.0f or %9.6f. At the end of this you will need a backslash with an n (\n) before you end the single quotes, to indicate that it should start a new line after. The % will always need to be there, but the number and the letter will change. Note that this % sign is not the same as a comment sign.
The number signifies the field width and precision. The number before the decimal is the field width, and the number after is the precision. The field width specifies how far apart the columns are in the table. The precision is the number of spots after the decimal place.
The letter signifies the conversion character, which help to format data into text. The conversion characters indicate the value types, for example an integer or a floating point value. I won’t get into the different value types and their definitions, but here is a table below. The Conversion column shows the different letters that can be used. %f is one of the more common conversion characters.
Running the original code (before the explanation section) gives this output:
Ex. 2: Formatting Text with One Variable
Aside from making a table, you can also format the output of your code into a sentence using fprintf.
Here’s an example. Create a code that asks the user for a color, then displays that input in a sentence that says “The color entered is _____.” In the blank space, the name of the input color will appear. How do we do this?
(1) First we will take input from the user that asks to “enter a color.” This will be set equal to the variable x so we can call the variable in the fprintf statement later on. Recall that we must use ‘s’ after the input sentence to indicate that the input will be a string and not numeric values.
(2) We will use fprintf to format the output with a sentence that reads fprint(‘The color entered is %s\n’, x). You need to put the sentence in quotes, and use %s. s is used as the conversion character because this is a string and not a numeric value. I didn’t specify any numbers like %2.0s, but MATLAB will default to its own formatting so this is fine. I also added that \n to the end before I closed the single quotes. After that, add a comma and then the name of the variable that will replace the %s in the sentence, which for this example was the variable x.
Putting this together gives the following:
Running the code shows this output. It prompts the user to enter a color:
I entered blue and you can see the output shows The color entered is blue.
Note that if you wanted a sentence to show more than one variable, you just have to enter the second variable into the fprintf line.
Ex. 3: Formatting Two or More Variables
If we want the output to display more than one variable in a sentence, we can enter it into the same fprintf line.
Here’s an example. Create a code to get the user to enter a fruit and its corresponding color (using two different input statements) and then output these into a sentence stating “the fruit is ___ and the color of the fruit is ____.”
Here are the steps to solving this:
(1) We need to create two input statements for the user to enter both a fruit and its color. There needs to be an ‘s’ within the input brackets to indicate to MATLAB that these are string inputs and not numeric values.
(2) We will create an input statement, the same as the one in Example 2, but we will change the sentence according to what we want in this questions – “the fruit is ___ and the color of the fruit is ____.” We will replace the blank spaces with %s, then end the fprintf brackets with the input variables from part (1).
Putting it all together gives:
Running the code gives the following. It prompts the user to enter a fruit:
I entered banana, and then it prompts the user to enter the color of the fruit:
I entered yellow as the color. Now the output statement shows “The fruit is yellow and the color of the fruit is yellow.”