How to Append Data to a File in MATLAB?


In MATLAB, appending is nothing but the process of adding data to a text file which already exists in the memory or workspace.

  • In MATLAB, there is a built-in function 'fprintf' which is used to append or add data to a file.

  • The 'fprintf' function allows users to add or writer formatted data to a text file. By default, this function adds the data to the end of the text file.

Let us now see the step-by-step process to append data to a file in MATLAB.

How to Append Data to a File?

The step-by-step process to append data to a text file in MATLAB is explained here:

Step (1) – Open the text file to which you want to append or write the data. For this, you can use the 'fopen' function.

Syntax

File = fopen('myfile.txt', 'a');

Here, the specifier 'a' is representing that the text file will open in the append mode.

Step (2) – Once the file gets opened, you can now append or write data to the file. For this, you can use the 'fprintf' function.

Syntax

fprintf(File, '%d
', data);

Here, 'data' is the data to be appended to the file, and '%d
' is the format specifier for integer data. However, there are several other types of data format specifiers exist which are given below.

Step (3) – Once the data appending is finished. Close the file by calling the 'fclose' function.

Syntax

fclose(File);

Format Specifiers

A format specifier is a code used to specify the format of a data. For example, %d is a format specifier that represent that the given data is an integer.

The following table gives all the format specifiers that we can use in the 'fprintf' function to specify the format of data.

Fomrat Specifier

Description

%d

Integer value

%f

Floating point number

%c

Single character

%s

String array

%e

Exponential value

Escape Sequences

Sometimes, we also use escape sequences withing the 'fprintf' function. Commonly used escape sequences are listed in the following table:

Escape Sequence

Description


To create a new line

\t

To insert horizontal tab space

\v

To insert a vertical tab space

\b

Backspace

\r

Carriage return

\

To insert single backslash

%%

To insert percentage character

Example

Now, let us see how to write MATLAB code to append data to a text file. For this, consider the example given below.

% MATLAB code to append data to a text file
% Open the text file
File = fopen('MyFile.txt', 'a');

% Create data that you want to append to the file
data = 'Tutorials Point is a best eLearning Platform.';

% Append data to the file
fprintf(File, '%s
', data); % Close the file fclose(File);

Output

To view the content of the file 'MyFile.txt', execute the following command.

type MyFile.txt

It will give the following result:

Tutorials Point is a best eLearning Platform.

Example

Let us now consider another example to append integer data to a text file.

% MATLAB code to append integer data to a text file
% Open the text file
File = fopen('MyFile.txt', 'a');

% Create data that you want to append to the file
data = [1, 2, 3, 4, 5];

% Append data to the file
fprintf(File, '%d\t %d\t %d\t %d\t %d\t', data);

% Close the file
fclose(File);

Output

To view the content stored in the file, run the following code.

type MyFile.txt

The output will be,

Tutorials Point is a best eLearning Platform.
1	 2	 3	 4	 5

Conclusion

This is all about appending data to a file in MATLAB. In conclusion, MATLAB provides a set of built-in functions namely, 'fopen' and 'fprintf' that we can use to append or write data to a text file. We can also specify the format specifier to format the data. In this tutorial, I explained the process of appending data to a text file with the help of examples.

Updated on: 07-Sep-2023

568 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements