Open In App

How to Append Data to a File in MATLAB?

Last Updated : 21 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Appending data to a text file means adding data to a file that already exists in the storage. print() function is used to write/append data to a file in MATLAB. It writes formatted text to a file based on the format string provided to it. The format of the output/input is determined by the formatting specifier. 

A format specifier is used to format ordinary text and special characters. It starts with a percent sign % and ends with a conversion sign. The different format specifiers used within fprintf function are:

%d : Display the value as an integer
%e : Display the value in exponential format
%f : Display floating point number
%g : Display the value with no trailing zeros
%s : Display string array (Unicode characters)
%c : Display single character(Unicode character)

Escape sequences used with fprintf function are:

\n  : create a new line
\t  : horizontal tab space 
\v  : Vertical tab space
\r  : carriage return
\\  : single backslash
\b  : backspace
%%  : percent character

Now before adding/appending data to a file, we have to ensure that the file exists. For that we will check whether the file exists or not using isfile function:

Example 1:

Matlab




% MATLAB code for create a file %
filename = "Geeks.txt";
  
if isfile(filename)
    % File exists
  
else
    % File doesn't exist


 

On the basis of the above control structure, we can add data to the file if it exists, or display an error if it doesn’t. Be aware that isfile searches for the given filename within the Current Working Directory of the MATLAB program. i.e. the file should exist in the same directory as the program. 

For file R/W operation we would be using the aforementioned fprintf function. Since MATLAB is predominantly used for operations performed over matrices, we would be appending a matrix to a file containing data regarding other matrices.

The file named Geeks.txt contains the following data:

 

we would be appending the following matrix to the file:

a = [7, 8, 9]

Example 2:

Matlab




% MATLAB code for append data in file %
filename = "Geeks.txt";
a = [7, 8, 9]
if isfile(filename)
    fid = fopen(filename, 'a+');
    fprintf(fid, '\n%3d %3d %3d', a);
    fclose(fid);
else
    disp("Error! File doesn't exist");
end


Output:

 

Explanation:

In the above code firstly the name of the file is saved into the variable filename. Then a 1D array was defined that is to be appended inside the file. After which the presence of the file is tested using the isfile function. If the file exists, then the file is opened via fopen function in append mode (using a+ flag) and its descriptor is saved into variable fid. Then the file descriptor, format string denoting the array (padding included) and the array to be appended is passed as an argument to the function. In the end, the file is closed. If the file doesn’t exist isfile equates to 0, and else the block is executed. Resulting in an error message being displayed. 

Note:

  • It is possible that the file may not open even if it is present, due to file locks, unauthorized access, low memory space, etc. In that case, the file descriptor will have the value -1 assigned to it, which could be checked to determine whether the file got opened or not (using an if statement). 
  • The second argument to the fopen function is a+ which tells the compiler that this file is to be opened in append mode. Other modes include read, write, binary, etc. 
  • The format specifier is based solely on what type of data is being appended to the file. In the case of strings, it would be %s for floating values it would be %f, etc. So an understanding of format strings is required to properly append data into the files.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads