Matlab

How to Read Formatted Data from Text File in MATLAB

While developing MATLAB applications and performing different string operations, we need to read data from a text file or character vector and store it in a cell array. MATLAB facilitates us with a built-in textscan() function that allows us to read formatted data from a text file or character vector.

Follow this tutorial to understand the functionality of the textscan() function with different syntaxes and examples for reading formatted data from text files in MATLAB.

How to Read Formatted Data from Text Files in MATLAB?

The textscan() is a built-in MATLAB function that is responsible for reading formatted data in any format using various specifiers like %e, %s, %d, and %f from a file or character vector. This function accepts a format specifier and fileID as mandatory inputs and stores the results in the cell array.

Syntax

We can use the textscan() function in MATLAB through the following syntaxes.

C = textscan(fileID,formatSpec)
C = textscan(fileID,formatSpec,N)
C = textscan(chr,formatSpec)
C = textscan(chr,formatSpec,N)

 

Here,

  • The function C = textscan(fileID,formatSpec) is responsible for reading data from a text file to a cell array C. The file identifier fileID is used for identifying the text file. We need to open the file using the fopen() function to obtain the value of fileID. After reading all data from the file, we need to close it using the fclose(fileID) function.
  • The function C = textscan(fileID,formatSpec,N) is responsible for reading data from a text file to a cell array C for N times where N represents a positive integer.
  • The function C = textscan(chr,formatSpec) is responsible for reading data from a character vector chr to a cell array C.
  • The function C = textscan(chr,formatSpec,N) is responsible for reading data from a character vector using fomatSpec N times to a cell array C. Where N represents a positive integer.

Example 1: How to Read Formatted Data from a Text File in MATLAB?

This MATLAB code opens a text file named cos_file.txt using the fopen() function and reads the formatted data from the file and stores it in the cell array C using the textscan() function. After that, it uses the celldisp(C) function to display data stored in the cell array C.

filename = 'cos_file.txt';
fileID = fopen(filename,'r');
C = textscan(fileID, '%q %d %f','Delimiter',',')
fclose(fileID);
celldisp(C)

 

Example 2: How to Read Formatted Data from Text File by Repeating Conversion Specifier in MATLAB?

In this example, we open a text file named cos_file.txt using the fopen() function, read the formatted data from the file, and store it in the cell array C by repeating the conversion specifier for N=5 times using the textscan() function. After that, we use the celldisp(C) function to display data stored in the cell array C.

filename = 'cos_file.txt';
fileID = fopen(filename,'r');
N = 5;
C = textscan(fileID, '%q %d %f',N,'Delimiter',',')
fclose(fileID);
celldisp(C)

 

Example 3: How to Read Formatted Data from Character Vectors in MATLAB?

In this MATLAB code, we use the textscan() function to read formatted data from the given character vector. After that, we use the celldisp(C) function to display data stored in the cell array C.

chr = ['l','i','n','u','x','h','i','n','t'];
C = textscan(chr,'%s')
celldisp(C)

 

Conclusion

MATLAB’s textscan() function enables us to read formatted data using any format specifier from a text file to a cell array. This guide has presented an easy and simple implementation of the textscan() function in MATLAB with examples that help us grasp the understanding of the function.

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.