Matlab

How to Read a Text File in MATLAB

MATLAB is a high-level programming environment using which we can perform multiple tasks such as data analysis, visualization, and machine learning. MATLAB allows us to take input from users in multiple forms such as a text file, or any other input data. This article will cover the MATLAB fscanf() and fopen() functions to read text files and display them on the command window.

fscanf() Function

The fscanf() in MATLAB can read data from a file. It reads formatted data from a file or the standard input, similar to the scanf() function in C.

The fscanf() function takes three arguments: the file handle, the format string, and the data variable. A file handle is a number that MATLAB uses to identify the file that we want to read. The format string tells MATLAB how to interpret the data in the file. The data variable is the variable where MATLAB will store the data that it reads from the file.

Syntax

A = fscanf(fileID,formatSpec)
A = fscanf(fileID,formatSpec,sizeA)
[A,count] = fscanf(___)

Read Text File Using fscanf() Function

Below MATLAB code uses the fscanf() function to read the content of a text file named abc.txt:

% Open the text file
fileID = fopen('abc.txt', 'r');

if fileID == -1
    error('Could not open the file.');
end

% Read the content of the file
fileContent = fscanf(fileID, '%c');

% Close the file
fclose(fileID);

% Display the file content
disp(fileContent);

Code starts by opening the text file in read mode after that using the fscanf() function the read content is stored inside the variable fileContent. The %c format specifier is used to read the content as a character array. The fclose() will close the current text file in MATLAB and disp() function will display file content on the screen.

Make sure that the abc.txt file is in the same directory as your MATLAB script or provide the full path to the file in the fopen() function if it is located elsewhere.

fopen() Function

The fopen() function in MATLAB can open a file. It returns a file identifier that can be used with other file I/O functions. This MATLAB function takes two input arguments: filename and mode of the file in which we need to open it. We have three different modes for file opening that includes: read mode (r), write mode (w), and append mode (a).

Syntax

fileID = fopen(filename)
fileID = fopen(filename,permission)
fileID = fopen(filename,permission,machinefmt,encodingIn)

Read Text File Using fopen() Function

Here’s a MATLAB code that reads the content of a text file named abc.txt:

% Open the text file
fileID = fopen('abc.txt', 'r');

if fileID == -1
    error('Could not open the file.');
end

% Read the content of the file
fileContent = fread(fileID, '*char')';

% Close the file
fclose(fileID);

% Display the file content
disp(fileContent);

Code started by using the fopen() function which opens the text file in read mode (r). The fread() function reads the content of the file and stores it in the variable fileContent. Next, the fclose() will close the current file, and using disp() function, the file content will be displayed on the MATLAB screen.

Conclusion

Using MATLAB functions and different commands we can read and display the text file content on screen. Some functions such as fscanf() and fopen() can read text files. Using these functions, one can easily open a file, read its content, and display it. MATLAB simplifies the process of working with text files, enhancing data analysis and processing capabilities. Read detailed steps about MATLAB text file reading in this article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.