Matlab

How Do You Use the fprintf Command in MATLAB

In MATLAB, the fprintf command is a powerful tool for displaying formatted output to the command window or writing data to a file. Users can regulate how data appears by defining formats, accuracy, and alignment. This article tries to provide a thorough explanation of the MATLAB fprintf command, including examples and its syntax.

How Do You Use the fprintf Command in MATLAB

To publish formatted data to a file or the command window in MATLAB, use the fprintf function. It enables you to control the output and set the format of the data being written. The basic syntax of the fprintf function is as follows:

fprintf(fileID, format, A)

Here, fileID refers to the identifier of the file where the output will be written. It can be a file opened using fopen or 1 to refer to the command window. The format represents the format specification string, which defines the layout of the output and lastly, the A denotes the data to be displayed or written.

To illustrate the usage of the fprintf command, I have given two examples:

Example 1: Displaying Formatted Output

Consider a scenario where we want to display the values of two variables, x, and y, along with their sum and product, below is the code to achieve this:

x = 5;
y = 10;
sum = x + y;
product = x * y;
fprintf('x = %d, y = %d\n', x, y);
fprintf('Sum: %d\n', sum);
fprintf('Product: %d\n', product);

Here, we directly use the fprintf command to print formatted output to the command window. The format strings %d and \n are used to print integers and newline characters, respectively. The values of x and y are supplied to fprintf as extra arguments:

Example 2: Specifying Floating-Point Precision

Here, we have a value of pi that we want to display with a precision of four decimal places and this is how it can be achieved:

pi_value = pi;
fprintf('Value of pi: %.4f\n', pi_value);

Here, the format string %.4f is used to specify a floating-point format with four decimal places. The variable pi_value is then passed as an argument to fprintf:

Example 3: Displaying Statistical Summary

Suppose we have a dataset containing exam scores for a class of students, and we want to display the statistical summary of the scores, including the minimum, maximum, mean, and standard deviation. Here’s how we can achieve this using fprintf:

scores = [78, 85, 92, 88, 95, 80, 83, 90, 87, 91];
fprintf('Statistical Summary:\n');
fprintf('Minimum: %d\n', min(scores));
fprintf('Maximum: %d\n', max(scores));
fprintf('Mean: %.2f\n', mean(scores));
fprintf('Standard Deviation: %.2f\n', std(scores));

In this example, we start by defining the scores array, which represents the exam scores of the students. Using fprintf, we display a header line Statistical Summary to indicate the content that follows.

We then employ a number of fprintf instructions to display the scores’ minimum, maximum, mean, and standard deviation. The format strings %d and %.2f are used to print integers and floating-point numbers with two decimal places, respectively.

The corresponding values are passed as additional arguments to fprintf using the min, max, mean, and std functions applied to the scores array. When executing this code, the statistical summary of the exam scores will be displayed in the command window, as follows:

This example illustrates how the fprintf command can be used to present statistical information in a clear and formatted manner, facilitating data analysis and interpretation.

Format Specifiers for fprintf Function

The format specifiers that can be used in combination with fprintf in MATLAB to format and print different types of data in the output stream are given in the table below. Remember to provide the corresponding argument(s) after the format string is printed.

Format Specifier Description
%d Signed decimal integer
%p Pointer address
%% Print a literal % character
%s String
%i Signed decimal integer
%X Unsigned hexadecimal integer (uppercase letters)
%u Unsigned decimal integer
%f Decimal floating point
%c Character

Conclusion

The fprintf command in MATLAB is a versatile tool for formatting and displaying output. By utilizing the appropriate format strings, precision, and alignment options, users can control the appearance of data. This article provides three different ways of using fprintf command in MATLAB.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.