The fprintf function allows you to "write" information to the screen for the user to view. This very important when user interaction is involved. The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read.

The fprintf function

The fprintf function is used for printing information to the screen. The fprintf function prints an array of characters to the screen:

	  fprintf('Happy Birthday\n');

We often use the fprintf statement to show the user information stored in our variables. To "place" a number into this string of printed characters we use several formatting options, but they all start with a % sign. It should noted that when used in this way, the % is not the comment character.

Below are several examples of printing information from variables using fprintf. Notice the use of %s to print a string, and %d to print an integer, and %f to print a number with a decimal (a floating point number). The Value of each variable is "plugged into" the appropriate format string at the appropriate location as marked by the % sign in the string

	  fprintf('My name is %s\n', prof_name);
	  fprintf('My age is %d and my salary is $%.2f\n', prof_age, prof_salary);

Again, the '%' notation in this case is not a comment, but is the "formating" of the string.

When using the % signs to print out the data stored in variables, we must use the same number of % signs as the number of variables.

Some common format options:

Above, the example "%.2f" is given. By putting a decimal point and a number in the format command, you are telling the program to print only two decimal places. This is perfect for printing dollar amounts, where we don't care about 1/1000 ths of a cent.

For more information, try "doc fprintf"

Alternative ways to print information

The following ways to "print" information are also available in Matlab, but are "frowned" upon, except for rapid debugging of a program.

  1. The display command:

    • disp is a function that will display a value
    • name = 'Jim';
    • disp(name);
    • alternative to the fprintf statement.
    • prefer to use fprintf as we control theFormatting
  2. Removing the ;

    Simply removing the ';' semicolon from the variable assignment:

    • forget the ';'
      	      name = 'Jim'                   
      	      name = 
    • Do not use this option, except for quick debugging

Again, please note: fprintf is the preferred method

When to print information.

Any time you print information, you should make sure it is of interest to the user of the program every time the code is run.. For example a function that compute pi should never use fprintf. Instead, such a function should return the value of pi for use by other parts of the program. If this approximated value of pi is of interest to the user, then the main program can print the result.

Proper use of fprintf to display the result of a function

	  function result = add(a,b)
	    result = a + b;

	>> the_sum = add(5,7);
	>> fprintf('the sum is %d\n', the_sum);

Improper use of fprintf to display the result of a function

	function result = add(a,b)
	  result = a + b;
	  fprintf('the sum is %d\n', the_sum);  % WARNING:  NOT WANTED

	>> the_sum = add(5,7);
	the sum is 12
	>> fprintf('the sum is %d\n', the_sum);
	the sum is 12

Notice how the second example prints "the sum is XX" every time the add function is called. In a program that adds millions of numbers, this would become quite annoying very quickly.

It is very important NOT to use fprintf in a function when the reason for using the function is to compute a value for the computer to use. It is very annoying to have miscellaneous printouts from un-related functions, when trying to compute a final value.

Back to Topics List