Real Technology Tools

fprintf MATLAB function: Beginner’s Guide

In this article, you’ll learn to generate text files from MATLAB using the MATLAB command fprintf. You’ll find out about:

MATLAB fprintf Syntax

How to use fprintf in MATLAB

In MATLAB, you can print text into a file by using the fprintf MATLAB command. To do this, go through the following 3 steps:

  1. Open a file using fopen.
  2. Write content using fprintf.
  3. Close the file using fclose.

For example, let’s write the word “Text” into a file named “textFileName.txt”:

fid = fopen('textFileName.txt', 'at');
fprintf(fid, 'Text');
fclose(fid);

If you run this piece of code in MATLAB, a file named ‘textFileName.txt’ will be created in your current folder:

Example of using the fprintf MATLAB command

Delete Existing Content When Writing

There are 2 ways to delete the content before writing when the file already exists:

  1. Either use the argument ‘w’ instead of ‘a’ in the fopen MATLAB command:
    fid = fopen('textFileName.txt', 'wt');
    fprintf(fid, 'Text');
    fclose(fid);
  2. Or, use the delete MATLAB command to delete the existing file before using fopen to create a new file with the same name:
    delete('textFileName.txt');
    fid = fopen('textFileName.txt', 'at');
    fprintf(fid, 'Text');
    fclose(fid);

If ‘textFileName.txt’ doesn’t exist, MATLAB will generate a warning; otherwise, it will delete the file before creating a new one with fopen.

Print Numbers in a Text File

You can format as follows:

delete('textFileName.txt');
fid = fopen('textFileName.txt', 'at');
fprintf(fid, ['%d + %d = %d'], 2, 3, 5);
fclose(fid);

Or, you can just use the num2str MATLAB command to convert your number into a text:

delete('textFileName.txt');
fid = fopen('textFileName.txt', 'at');
fprintf(fid, [num2str(2) '+' num2str(3) '=' num2str(5)]);
fclose(fid);

Warning: File not Found or Permission Denied

When you encounter a warning saying that the file cannot be overwritten, it could be because there have been some issues with closing the file the last time it was opened:

In this case, you can close it by using:

fclose all

After running this command you should be able to delete/overwrite the file.

MATLAB fprintf: Matrix

Let’s say we have a matrix:

\(m = \begin{pmatrix}1 & 2 & 3\\4 & 5 & 6\end{pmatrix}\)

It can be defined in MATLAB as follows:

m = [1 2 3 ; 4 5 6];

The intuitive way to write a matrix would be to use “%d” for each column of the matrix such as:

fid = fopen('textFileName.txt', 'at');
fprintf(fid, '%d %d %d\n', m);
fclose(fid);

However, because fprintf doesn’t store matrices in an intuitive way, in MATLAB you’d get:

Instead, you need to use the transpose of that matrix:

fid = fopen('textFileName.txt', 'at');
fprintf(fid, '%d %d %d\n', m'); % notice the single quotation mark at the right of m
fclose(fid);

Now, you get:

MATLAB fprintf: Complex Number

If you want to print complex numbers, there is no specific format, you need to separate the real from the imaginary part:

x = 1+3i;
fid = fopen('textFileName.txt', 'at');
fprintf(fid, '%d+%dj\n', real(x), imag(x));
fclose(fid);

Here’s what the text file will look like:

Key takeaways:

  1. The basic structure to write text into a file is the following:
    fid = fopen(fileName, 'at'); % a is for append
    fprintf(fid, text);
    fclose(fid);
  2. To delete the content in the file before writing using the syntax:
    1. Use “w” as an argument of fopen:
      fid = fopen(fileName, 'wt'); % w is for write
      fprintf(fid, text);
      fclose(fid);
    2. Use the delete MATLAB command:
      delete(fileName);
      fid = fopen(fileName, 'at');
      fprintf(fid, text);
      fclose(fid);
  3. To print a matrix in MATLAB, you need to transpose the matrix:
    fid = fopen('textFileName.txt', 'at'); 
    fprintf(fid, '%d %d %d\n', m'); % notice the single quotation mark at the right of m 
    fclose(fid);
  4. To print a complex number in MATLAB, separate the real from the imaginary part:
    x = 1+3i; 
    fid = fopen('textFileName.txt', 'at'); 
    fprintf(fid, '%d+%dj\n', real(x), imag(x)) 
    fclose(fid);

To learn more about fprintf, check out the following links:

If you want to learn more about the tools that helped me stop wasting time doing mindless work (such as generating Excel reports, Word documents, or creating clean and simple user interfaces) I wrote a small reference book about it:

 

 

Exit mobile version