Part 1: Preparing Data Set to be Read Into Matlab

Matlab Logo Often times in Matlab we would like to read data from a *.txt file. The importdata function is convenient as long as you don’t have text or an inconsistent number of columns in your data set. However, if you’re dealing with large volumes of data, it is inconvenient to delete the header content by hand. One option is to use php to automatically remove the contents in all your files. However, most data sets that are generated from data acquisition systems will usually put header content that provide key information on measurement parameters such as: number of points, sampling rate, etc. Thus, we need to be able to extract this key information as well! In this tutorial I will show you how you can easily read in data files with header content.

Suppose your data file looks something along the lines of

1
2
3
4
5
6
7
8
9
10
11
n := 9
d := 5
k := 4
param p:1 2 3 4 5 :=
1 0.4 0.5 0.6 0.12 0.6
2 0.4 0.5 0.6 0.12 0.6
3 0.1 0.2 0.4 0.22 0.3
4 0.2 0.3 0.3 0.12 0.6
5 0.3 0.4 0.2 0.42 0.4
6 0.2 0.5 0.6 0.12 0.6
... data set continues

n = number of data points, d = number of data columns, and k = whatever other parameters you might need. You can read in the data using importdata on the input file as it is, but be warned that Matlab will not store the data as you would expect. Matlab will read in the data and store it like a matrix and treat the whole thing as a matrix, including your text! So the way to get around this is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 % Read in File
id = fopen('filename.txt');
 
% Read in Header Content
for i=1:3
    readin = fgetl(id);
    para(i) = str2num(readin(6:length(readin)));
end
fgetl(id);
 
% Read in Data
for i=1:para(1)
    data(i,:) = str2num(fgetl(id));
end
fclose('all')

Don’t worry, I’ll explain how the code works now.

id = fopen('filename.txt');

The function fopen creates a file identifier (numerical name tag) that Matlab uses for its functions. It essentially “opens” the file within Matlab, allowing Matlab to start reading in the file. The file is read in line by line using the function fgetl. You can remeber it as “get line”. To use the fgetl function, just call the function on the file identifier value of the input file. In this case, the file identifier is id. Each time you call the fgetl function, Matlab will automatically move onto the next line in the file, so that you don’t keep reading in the same line!

Pages: 1 2 3