Read text file in MATLAB

If you would like to analyze data generated from other sources, you will most likely need to import the data from a text, csv, or xls file.  Here we’ll provide different methods you can use to import this data.

First, you will need to select the file of interest, with the filename stored in the variable ‘file’:

% Select file
[filename, pathname] = uigetfile ('*.*', 'Pick a file');
file = [pathname, filename];

In our example, we will be importing a tab-delimited textfile generated by Sensor Data. The data file we are playing with is test_gyroscope_t3.txt.  As you can see, this file has 13 header lines (two of which are blank lines) and 110 lines of data (consisting of time, x axis, y axis, z axis).

Now that your file has been selected, you have several options through which to import.  First let’s look at textread. While this function will soon be decremented, it still serves a nice purpose. We can specify a delimiter, and then parse out the header and data using cellfun.

% Using textread, which will be removed in later MATLAB releases
% Read in file
allData = textread(file, '%s', 'delimiter', '\n');
% Make allData cells empty if numerical 
numericalArray = cellfun(@(s) sscanf(s,'%f').' ,allData, 'un', 0);
% Get Header
header = allData(cellfun('isempty',numericalArray));
% Get Data
data = vertcat(numericalArray{:});

With this piece of code, you will have a 11×4 cell array for the header and a 110×4 double for the numerical data. What if we were to use textscan as recommended by MATLAB rather than textread? Well now the code is a little bit longer, and you need to specify before hand how many columns of data you want as well as how many header lines are in the file. This method might be useful if you want to manually select only certain columns and even start at a later row, but does require knowledge of your data file. Here we’ll initialize the values for the headerlength and columnlength to 13 and 4, respectively. You’ll also notice our usage of the command repmat, which comes in very handy here and elsewhere.

% If you know the header and column length using textscan
headerLength = 13;
numColumns = 4;
fid = fopen(file);
% this is error message for reading the file
if fid == -1 
    error('File could not be opened, check name or path.')
end
% Get header
header = cell(headerLength, 1);
for i = 1:headerLength
    header{i} = fgetl(fid);
end
fclose(fid);
fid = fopen(file);
% Read in numerical data
data = textscan(fid, repmat('%f',1,numColumns), 'headerlines', headerLength);
data = cell2mat(data);
fclose(fid);

Again, our data has been stored into 11×4 cell array for the header and a 110×4 double for the numerical data. So this is great, now you can read in data… but wait, MATLAB also has a great function, which we just found that allows you to do all this with just a single line of code!

% importdata
A = importdata(file);

Importdata! This command will output the data to a struct, with a field for the data (110×4 double), textdata (11×4 cell) and colheaders (1×4 cell). The colheaders is simply the title for each column of data, in our case ‘Time’, ‘X’, ‘Y’, ‘Z’. You can even specify the delimiter for the file as the second argument to importdata, though the default character is interpreted from the file.

Regardless of which method you choose, you should now have access to your all-important data within MATLAB, so you can now begin analysis.

If you have found any other methods for importing data into MATLAB, please let us know.

4 thoughts on “Read text file in MATLAB

  1. trying it out on your file it looks like it did the right thing without even needing to tweak the command:

    >> T = readtable(test_gyroscope_t3.txt’)
    T =

    Time X Y Z
    __________ ___________ ___________ ___________

    1.4848e+12 -0.0006325 0.0041279 0.00026632
    1.4848e+12 -0.002763 0.0030626 -0.0029295
    1.4848e+12 -0.0038283 0.0019974 -0.0029295
    1.4848e+12 -0.0038283 -0.00013316 0.00026632
    1.4848e+12 -0.0070241 -0.00013316 0.0023968
    1.4848e+12 -0.0059588 -0.00013316 -0.00079895
    1.4848e+12 -0.0048936 -0.00013316 0.00026632
    1.4848e+12 -0.002763 0.00093211 0.0023968
    1.4848e+12 -0.0006325 0.00093211 -0.0039947
    1.4848e+12 -0.0006325 -0.00013316 -0.00079895
    1.4848e+12 0.00043276 -0.0011984 -0.0029295
    1.4848e+12 0.001498 -0.00013316 -0.00079895
    1.4848e+12 0.0046938 -0.00013316 0.00026632
    1.4848e+12 0.0036286 -0.00013316 0.0055926
    1.4848e+12 0.00043276 -0.0011984 0.0023968
    1.4848e+12 -0.0006325 -0.003329 0.00026632
    1.4848e+12 -0.0048936 -0.0054595 0.0013316
    1.4848e+12 -0.0059588 -0.0054595 0.0045274
    1.4848e+12 -0.0059588 -0.0054595 0.0045274
    1.4848e+12 -0.0059588 -0.0043942 -0.00079895
    1.4848e+12 -0.0038283 -0.003329 0.0013316
    1.4848e+12 -0.0016978 -0.0022637 -0.0029295
    1.4848e+12 0.001498 -0.0011984 -0.00079895
    1.4848e+12 0.00043276 -0.00013316 -0.00079895
    1.4848e+12 0.001498 0.00093211 -0.0018642
    1.4848e+12 0.00043276 0.0019974 0.00026632
    1.4848e+12 0.00043276 0.0030626 0.0013316
    1.4848e+12 -0.0006325 0.0030626 0.00026632
    1.4848e+12 0.00043276 0.0030626 0.00026632
    1.4848e+12 0.0025633 0.0030626 0.0013316
    1.4848e+12 0.0025633 0.0030626 -0.0061253
    1.4848e+12 0.0036286 0.0030626 -0.0061253
    1.4848e+12 0.001498 0.0041279 -0.0018642
    1.4848e+12 -0.0006325 0.0030626 0.00026632
    1.4848e+12 -0.002763 0.0030626 -0.0029295
    1.4848e+12 -0.0048936 0.0062584 0.00026632
    1.4848e+12 -0.0038283 0.0051932 0.0023968
    1.4848e+12 -0.0070241 0.0041279 0.00026632
    1.4848e+12 -0.0038949 0.0015313 -0.0017643
    1.4848e+12 -0.0017643 0.00046605 0.003562
    1.4848e+12 -0.0028296 -0.0016645 0.00036618
    1.4848e+12 -0.00069908 -0.00059921 0.0014314
    1.4848e+12 0.0024967 -0.0016645 -0.00069908
    1.4848e+12 0.0024967 -0.0016645 -0.00069908
    1.4848e+12 0.0046272 -0.00059921 0.00036618
    1.4848e+12 0.0024967 0.00046605 -0.0028296
    1.4848e+12 0.0014314 0.0025966 -0.0028296
    1.4848e+12 -0.0017643 0.0025966 0.0014314
    1.4848e+12 -0.0028296 0.0025966 0.0014314
    1.4848e+12 -0.0038949 0.0015313 0.0024967
    1.4848e+12 -0.0028296 -0.00059921 0.003562
    1.4848e+12 -0.0028296 -0.00059921 0.003562
    1.4848e+12 -0.0038949 -0.0027297 0.0014314
    1.4848e+12 -0.0028296 -0.0048603 -0.0028296
    1.4848e+12 -0.0028296 -0.003795 0.00036618
    1.4848e+12 -0.0028296 -0.003795 0.0046272
    1.4848e+12 0.00036618 -0.0027297 -0.00069908
    1.4848e+12 -0.00069908 -0.0016645 -0.0017643
    1.4848e+12 0.0014314 -0.00059921 -0.0017643
    1.4848e+12 0.00036618 -0.00059921 -0.0028296
    1.4848e+12 0.00036618 -0.00059921 0.00036618
    1.4848e+12 0.0014314 -0.00059921 0.0046272
    1.4848e+12 0.0014314 -0.00059921 0.0046272
    1.4848e+12 0.0024967 -0.00059921 0.0014314
    1.4848e+12 0.0014314 -0.00059921 -0.0017643
    1.4848e+12 0.0024967 0.00046605 -0.0038949
    1.4848e+12 0.00036618 0.0015313 0.00036618
    1.4848e+12 0.00036618 0.0015313 -0.0038949
    1.4848e+12 -0.0017643 0.0015313 -0.010286
    1.4848e+12 -0.0013316 0.00089882 -0.00059921
    1.4848e+12 -0.00026632 0.00089882 0.0015313
    1.4848e+12 -0.00026632 0.00089882 0.00046605
    1.4848e+12 0.0018642 -0.00016645 0.0036618
    1.4848e+12 0.0029295 0.00089882 -0.0016645
    1.4848e+12 0.00506 0.0019641 -0.00059921
    1.4848e+12 0.0029295 0.00089882 -0.0027297
    1.4848e+12 0.0029295 0.0019641 0.0025966
    1.4848e+12 0.0039947 -0.00016645 0.0036618
    1.4848e+12 0.00079895 -0.0012317 -0.003795
    1.4848e+12 0.0018642 -0.0012317 -0.0016645
    1.4848e+12 0.0018642 -0.0012317 -0.0016645
    1.4848e+12 0.0018642 -0.002297 0.0015313
    1.4848e+12 0.0029295 -0.00016645 0.0015313
    1.4848e+12 0.0018642 -0.00016645 0.0015313
    1.4848e+12 0.0018642 -0.00016645 -0.00059921
    1.4848e+12 0.0018642 -0.00016645 -0.00059921
    1.4848e+12 0.00079895 -0.00016645 -0.00059921
    1.4848e+12 0.00079895 -0.002297 0.00046605
    1.4848e+12 -0.00026632 -0.002297 0.0015313
    1.4848e+12 -0.0013316 -0.0033622 0.0025966
    1.4848e+12 0.00079895 -0.002297 -0.00059921
    1.4848e+12 0.00079895 -0.0033622 -0.0016645
    1.4848e+12 0.00079895 -0.0033622 0.0068576
    1.4848e+12 0.00079895 -0.0033622 0.00046605
    1.4848e+12 0.00079895 -0.002297 -0.0016645
    1.4848e+12 0.0018642 -0.00016645 -0.0016645
    1.4848e+12 0.0018642 -0.00016645 -0.0016645
    1.4848e+12 0.00079895 0.00089882 -0.003795
    1.4848e+12 0.00079895 0.0019641 -0.0016645
    1.4848e+12 0.00079895 0.00089882 0.00046605
    1.4848e+12 0.00079895 -0.00016645 -0.0027297
    1.4848e+12 -0.0006325 0.00043276 -0.00056592
    1.4848e+12 -0.0006325 0.001498 0.00049934
    1.4848e+12 -0.0006325 0.001498 -0.0026965
    1.4848e+12 0.00043276 0.0025633 -0.004827
    1.4848e+12 0.001498 0.0046938 0.0047604
    1.4848e+12 -0.0038283 0.00043276 -0.0026965
    1.4848e+12 0.0025633 0.0078896 -0.004827
    1.4848e+12 -0.002763 -0.0006325 0.00049934
    1.4848e+12 0.00043276 0.0036286 -0.0016312

Leave a Reply

Your email address will not be published. Required fields are marked *