Matlab

How to Write Data to a Text File in MATLAB Using fprintf() Function?

The fprintf() is a MATLAB function used for displaying the output on the command window. This function also allows us to write the obtained output in a text file to save it for various purposes. This saved output can also be displayed on the command window.

Follow this guide to learn how to use the fprintf() function to write the data in a text file in MATLAB.

What is the fprintf() Function in MATLAB?

The fprintf() is a built-in MATLAB function used for displaying the output on the screen. This function allows users to present the data in a well-formatted and desired way, making it easier to understand. It is also used for writing the formatted output in a file, allowing you to save the data for future use. The reason is this data will remain accessible and easily can be shared with anyone.

Syntax
The fprintf() function follows a simple syntax that is given below:

fprintf(fileID,formatSpec,A1,...,An)

Here,
The fprintf(fileID,formatSpec, A1,…, An) writes the data stored in the variable A1, A2,…,An in the text file fileID using the format specifiers.

How to Write-Output in a Text File in MATLAB Using the fprintf() Function?

Writing data in a text file is an essential task that can store information in a structured and easily readable format. This helps with efficient data management, sharing, and analysis. You can use the fprintf() function in MATLAB to easily write data in a text file using the following steps:

i: Store the data in one or more variables.

ii: Then use the fopen() function to open a file in which we will write the data.

iii: Identify whether the file is open or not using an if statement. If the file is not opened, throw an error message by comparing the file identifier with -1.

iv: Use the fprintf() function for writing the data into the text file.

v: Use the fclose() function to close the file for freeing the system resources.

vi: Display the message on the screen, if the process completes successfully.

Examples

The following example demonstrates the working of the fprintf() function in MATLAB to write data to text file using the above-given steps:

Example 1: How to Write a String in the Text File Using MATLAB’s fprintf() Function?

In the below-given example, we follow the above-given steps to write a given string x in the text file TextFile1.

x = 'Welcome to linuxhint';
file1 = fopen('TextFile1.txt', 'w');
if file1 == -1
   error('Failed to open the file.');
end
   fprintf(file1, '%s ', x);
fclose(file1);
disp('Data has been successfully written to the text file.');

We can display the file’s contents on the screen using the type command followed by the name of the text file.

type TextFile1.txt;

Example 2: How to Write a Matrix in the Text File Using MATLAB’s fprintf() Function?

This example uses the fprintf() function to write a given matrix A in the text file cos_file.

x = -pi/2:pi/10:pi/2;
A = [x; cos(x)];
fileID = fopen('cos_file.txt','w');
if fileID == -1
   error('Failed to open the file.');
end
fprintf(fileID,'%6s %12s\n','x','cos(x)');
fprintf(fileID,'%6.2f %12.8f\n', A);
fclose(fileID);
disp('Data has been successfully written to the text file.');

The content of the file can later be displayed using the type command with the file name in the command window.

type cos_file.txt

Example 3: How to Write the Numeric Data in the Text File Using MATLAB’s fprintf() Function?

The given MATLAB code writes the numeric data stored in A in the file random_num.txt by following the above-given steps.

A = rand(5, 4);
fileID = fopen('random_num.txt','w');
if fileID == -1
   error('Failed to open the file.');
end
fprintf(fileID,'%d %d %d %d\n',A);
fclose(fileID);
disp('Data has been successfully written to the text file.');

Display the file’s contents on the screen using the type command on the command window.

type random_num.txt

Conclusion

The fprintf() is a built-in function in MATLAB that displays the output on the screen. This function also has the ability to write the formatted data in the text file using the format specifiers. This tutorial has explained the basics of using the fprintf() function in MATLAB, helping you write data to a text file in a quick way.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.