Shirley Ideas All articles

Mastering MATLAB: Achieving Precision with fprintf

By Shirley Sep 05, 2026
a computer screen with an image of a diagram in the middle and a line drawing on the bottom
a computer screen with an image of a diagram in the middle and a line drawing on the bottom

MATLAB, a high-level language developed by MathWorks, offers a versatile and dynamic environment for numerical computation and advanced analytics. When working with file I/O operations in MATLAB, fprintf is a fundamental function that enables users to write formatted data to both command window and external files. This article delves into the functionalities, syntax, and practical applications of fprintf in MATLAB.

a computer screen with an image of a diagram in the middle and a line drawing on the bottom
a computer screen with an image of a diagram in the middle and a line drawing on the bottom

Before diving into the details, let's first consider a basic example: writing a simple message to the command window using fprintf.

the matlab logo on a white background
the matlab logo on a white background

Understanding fprintf Syntax

At its core, the fprintf function follows a straightforward syntax:

the matlab logo on a blue background
the matlab logo on a blue background

fprintf(format_string, var1, var2, ..., varN)

The function takes a format string and one or more variables as inputs. The format string dictates how the variables should be displayed, similar to the printf family of functions in C.

MATLAB
MATLAB

Format Specifiers

Format specifiers are placeholder characters within the format string that indicate how data should be displayed. Some of the most common specifiers include:

  • '%d': For integers.
  • '%f': For floating-point numbers.
  • '%s': For string data.
  • '%c': For character output.
Ultimate MATLAB Programming Cheat Sheet | Basics & Logic
Ultimate MATLAB Programming Cheat Sheet | Basics & Logic

Width and Precision

Format specifiers can also include width and precision fields to control the display. For example, '%5d' will print an integer in a minimum of 5 characters, padding with spaces if necessary. '%6.2f' will print a floating-point number to 2 decimal places within a field of 6 characters.

Writing to External Files

Learn Matlab
Learn Matlab

In addition to printing to the command window, fprintf can also write data to external files. To accomplish this, you must open the file using either 'wt' (for text files) or 'wb' (for binary files) mode with the fopen function before using fprintf.

Here's an example that writes a simple message to a text file named 'output.txt':

Matlab-Projects | Academic College Projects
Matlab-Projects | Academic College Projects
Reddit Answer: Can I Embed a [MATLAB] Live Script in a WordPress Blog?
Reddit Answer: Can I Embed a [MATLAB] Live Script in a WordPress Blog?
an abstract image of a red and orange arrow on a blue, yellow, and pink background
an abstract image of a red and orange arrow on a blue, yellow, and pink background
Fast Fourier Transform - MATLAB & Simulink
Fast Fourier Transform - MATLAB & Simulink
the logo for matlab is displayed on a red and blue background with an orange triangle
the logo for matlab is displayed on a red and blue background with an orange triangle
an image of a computer screen showing the flow diagram for a machine that is running
an image of a computer screen showing the flow diagram for a machine that is running
MATLAB Programming
MATLAB Programming
MATLAB Logo
MATLAB Logo
Representing Data as a Surface - MATLAB & Simulink
Representing Data as a Surface - MATLAB & Simulink
10 Most Useful Engineering Softwares You Must Learn
10 Most Useful Engineering Softwares You Must Learn

fileID = fopen('output.txt', 'w');
fprintf(fileID, 'Hello, World!\n');
fclose(fileID);

Formatting Data in Files

You can format data in external files in the same way as you would in the command window. Here's an example that writes formatted data to a text file:

fileID = fopen('data.txt', 'w');
data = [1, 2, 3, 4, 5];
fprintf(fileID, 'Integers:\n');
fprintf(fileID, '%d\t%d\t%d\n', data(1:3));
fprintf(fileID, 'Floating-point:\n');
fprintf(fileID, '%.2f\t%.2f\t%.2f\n', data(4:end).*2.5);
fclose(fileID);

Reading Data from Files

After writing data to a file using fprintf, you can read it back into MATLAB using fgetl, fgets, or fscanf functions. Here's an example using fscanf:

fileID = fopen('data.txt', 'r');
fscanf(fileID, '%s'); % Skip the first line of integers
A = fscanf(fileID, '%d\t%d\t%d\n');
B = fscanf(fileID, '%.2f\t%.2f\t%.2f\n');
fclose(fileID);

Practical Applications of fprintf

fmtlib, a MATLAB File Exchange contribution, leverages fprintf to provide additional file I/O formatting capabilities. For example, the printmatrix function in this toolbox can print matrices to a file in a variety of formats, including LaTeX, PDF, and HTML.

In conclusion, fprintf is a powerful tool for writing formatted data to both MATLAB's command window and external files. By mastering its syntax and format specifiers, you can streamline your data output and improve the readability of your code. Happy computing!