Outputting to the Screen

There are a few ways to output information to the screen. You may want to output information to the screen for multiple reasons, including for feedback. Feedback is usually output from a system for you to make sure that everything is operating as it should. Here, we shall explore the disp() command, the fprintf() command and also how to use those to provide feedback.

 

2.5.1 The disp() command
The disp() command comes in three different forms: one that outputs string, one that outputs a variable and one that outputs an array. There are a few more simple examples below to expose you to what else you can do with the disp() command:

disp(string): disp('The values in the matrix are: ')
disp(name): disp(x)
disp(array): disp(['The values in the x array are: ' , num2str(x)])

 

You may have noticed that it may be cumbersome to type in the same code over and over again (especially with the disp() command), and this is where a much better and more customisable command comes into play. You may also want a formatted output which is more presentable than having everything output separately. This is where the fprintf() command comes into play.

 

2.5.2 The fprintf() command
With the generalised syntax of fprintf(string, variable1, variable2, ...) the fprintf() command allows you to produce formatted outputs in a format that you are free to set.

For example, what if we wanted MATLAB to output your information in Exercise 1.3 in the following format: “Your name is [your name]. You are [your age] years old and you live in [your country]”? The fprintf() command will do this for you. For example:

fprintf('Your name is %s. You are %0.0f years old and you live in %s.\n', name, ageYears, country);

 

You may be wondering what the percentages signify. %s tells MATLAB to input a string in that location. %f may be formatted to display numbers at a certain width from what precedes them. For instance, in this case, %0.0f means that the number should immediately follow what preceded it (0 spacing from the front of the number and what precedes it, which is a space in this case), and that it should contain 0 decimal places.

\n is a control that is used to tell MATLAB to start a new line after outputting what fprintf() tells it to output. What follows \n will be the variables that appear in order of the formatting operators (%s and %f in this case) so that MATLAB knows where to put each variable.

Attached is fprintf_example.m (within the zip file) which you can run to understand how the operators work, and how fprintf() works.

Download:

 

Suppose you had data that contained 5 decimal places but that you wished to show as accurate as 2dp. You can use fprintf() to control how many decimal places you want your data accurate to. For example:

reading = 23.09898;
fprintf('Your reading is %0.2f deg C. \n', reading);

 

This will output: Your reading is 23.10 deg C.

If you wanted to add a couple of spaces before 23.10, you could modify the code to increase the width of the output, hence delaying the starting point of the number. For example:

fprintf('Your reading is %8.3f deg C. \n', reading);

 

%8.3f means that the first value in the output has a field width of eight digits, with three digits after the decimal. This will display:
“Your reading is   23.099 deg C.”.

Note that there are three decimal places, and that the width of the field starting from the % symbol, until the end of the number is 8 characters: “is     2 3 . 0 9 9 deg”.

Those codes are included in the fprintf_variations.m file that you can download here (within the attached zip file). Feel free to play around with it and to try out other operators that can be found in the MATLAB help documentation by doing a simple search for fprintf(), as shown by Figure 2.6.

Download:

 

Figure 2.6

Figure 2.6

 

There are a few other formatting operators that can be used, including %e and %g. %f represents decimal notation, while %e is exponential and %g is whichever of them is shorter.

Instead of using \n to display results on separate lines, you may want to use \t to use a tab, or \b to backspace.

Also learn about the sprintf command from the documentation provided.

 

2.5.3 Using disp() or fprintf() to provide feedback
It is sometimes very important to be able to get your code to provide feedback so you can check if you’ve made a mistake. You can do this using a few methods, including:

  • Printing them using the disp() or fprintf() commands
  • Calling them
  • Performing operations using them and checking the results

If you have a lot of code, simply calling the variables that you would like to check, or performing operations on them, may be misleading and may also be confusing (because you might not know what the outputted values relate to). Your best option would be to print the variables using one of disp() or fprintf() to the screen so that MATLAB can tell you specifically what each value relates to.

For example, say you declared a variable, age to hold a value of 21:

age = 21

 

Say you performed an operation on age a year on, so that it can hold a value of 21. Instead of changing its value manually, say you coded the change so that it could take place immediately, for example:

birthYear = 1994
currentYear = 2016
age = currentYear - birthYear

 

It may be helpful to have MATLAB output text with the variable so that you can understand what the printed value represents, for example:

disp(['The age variable holds a value of ' , num2str(age) , '.'])

 

OR

fprintf('The age variable holds a value of %0.0f. \n', ageYears);

 

The disp() command is good enough in this case, however when you have multiple outputs, the fprintf() command may save space. If you would like feedback to be shown on separate lines though, then the disp() command is sufficient.