November 22, 2024

Import *.csv and *.txt Numerical Files and Data in MATLAB

In this post, we explain how to import *.csv and *.txt numerical files and data in MATLAB. The YouTube video accompanying this post is given below.

First, we write a C code that writes a txt file with numerical values. This code is explained in our previous post, which can be found here. The C code is given below.

#include <stdio.h>
// this file explains how to save data to a text file in C
int main()
{
	//file name that is used to store the data
	// this is a string literal
	char const *file_name="data_file.txt";
	int j=0; // not allowed in the for loop to define a counter 
	// if you declare j in the loop, you will get this error: [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode
	
	// open the file for writting, "w" option will delete the content of the file
	FILE *file=fopen(file_name,"w");
	
	if (file==NULL)
	{
		printf("Could not open the file %s",file_name);
		return 0;
	}
	
	for (j=0; j<10; j++)
	{
		fprintf(file,"%d,%f\n", j, (float)1.2*j);
	}
	
	
	fclose(file);
	
	return 1;
}

Before reading the rest of the code, you should compile and run this code. The generated text file has the following form:

0,0.000000
1,1.200000
2,2.400000
3,3.600000
4,4.800000
5,6.000000
6,7.200000
7,8.400001
8,9.600000
9,10.800000

There are at least 3 approaches that can be used to import this numerical file in MATLAB. The first approach is to use the MATLAB function “dlmread()”. We can import the data by using the following code line

A=dlmread('data_file.txt')

The next approach is to use the MATLAB function “csvread()”. We can import the data by using the following code line

B=csvread('data_file.txt');

The third approach is to use the function “readmatrix()”. This function is introduced in the 2019 version of MATLAB. We can import the data by using the following code line

C=readmatrix('data_file.txt')

Now, let us make our life a little bit more complicated. We manually add two text rows to our text file (YouTube video given at the top of this post explains how to modify the text file). The modified “data_file.txt ” file has the following form:

column1,column2
0,0.000000
1,1.200000
2,2.400000
3,3.600000
4,4.800000
5,6.000000
6,7.200000
7,8.400001
8,9.600000
9,10.800000
textValue1,textValue2

Basically, we have a header row that is:

“column1,column2”

and we have the last text row that is:

“textValue1,textValue2”

Let us now repeat the read procedure

A=dlmread(‘data_file.txt’)

The MATLAB result is

Error using dlmread (line 147)
Unable to parse a "Numeric" field when reading row 1, field 1.
   Actual Text: "column1,column2"
   Expected: A number or literal "NaN", "Inf". (possibly signed, case insensitive)

Consequently, the text rows create an issue for dlmread() function. Now, let us try the second approach

B1=csvread(‘data_file.txt’)

The MATLAB output is

Error using dlmread (line 147)
Unable to parse a "Numeric" field when reading row 1, field 1.
   Actual Text: "column1,column2"
   Expected: A number or literal "NaN", "Inf". (possibly signed, case insensitive)

Error in csvread (line 48)
    m=dlmread(filename, ',', r, c);

So it seems that csvread() function is calling the function dlmread(), and dlmread() reports the previously seen error. Let us now try our third option, that is based on the function readmatrix():

C1=readmatrix(‘data_file.txt’)

The MATLAB output is

         0         0
    1.0000    1.2000
    2.0000    2.4000
    3.0000    3.6000
    4.0000    4.8000
    5.0000    6.0000
    6.0000    7.2000
    7.0000    8.4000
    8.0000    9.6000
    9.0000   10.8000
       NaN       NaN

So this function is able to read the file, and ignore the first row consisting of text. However, the last row consists of NaN values. Consequently, the text is converted to the NaN values. There is an approach to deal with this issue. However, we do not explain it in this video. Instead, we add additional parameters to the function dlmread(), to correctly read the data. The modified code line takes the following form

M1 = dlmread('data_file.txt',',',[1 0 10 1]) % the last argument is [R1 C1 R2 C2], row and column offsets

The last argument [1 0 10 1] specifies the range of columns and rows to be included while reading the file. The general form is [start_row, start_column, end_row, end_column].

The third parameter ‘,’ (comma), denotes a delimiter between the numerical values. Here, the rows and columns are indexed from 0. For example, row 1 in the text file is indexed by 0. The file has in total 12 rows and two columns (excluding the delimiter character). We read from the second row, until the row 11, and we read all two columns. Consequently, our row vector denoting the range of columns and rows that need to be read has the following form ‘[1 0 10 1]’. The resulting matrix M1 has the following form

M1 =

     0         0
1.0000    1.2000
2.0000    2.4000
3.0000    3.6000
4.0000    4.8000
5.0000    6.0000
6.0000    7.2000
7.0000    8.4000
8.0000    9.6000
9.0000   10.8000

and this content is equal to the content of our original file.