Search code examples
matlabtextscanformat-string

Read formated data from .txt to MATLAB correctly


Consider having a .txt file (it's name is saved as variable fName) with data (2 header rows, 5 rows and 2050 columns with data: 1-st column is Time in format hours:minutes:seconds.miliseconds, 2-nd column is a timestamp, all the rest columns are a matrix of numeric data.

File is temporarily available from here. Preview of the file contents (6 columns of 2050):

My data
Number of Pixels per Row: 2048
12:23:14.305    1435742594305   -1.39   1.61    0.61    3.61    ...
12:23:14.815    1435742594815   -1.56   -1.56   -1.56   2.44    ...
12:23:15.326    1435742595326   -0.17   0.83    -0.17   4.83    ...
12:23:15.837    1435742595837   -0.22   -0.22   -2.22   0.78    ...
12:23:16.351    1435742596351   -1.17   -0.17   -1.17   4.83    ...

I use the following code to read the data:

skipRows = 2;

% # Read file:

fileID = fopen(fName,'r');
RawData = textscan(fileID,['%s\t','%f\t',repmat('%f\t',1, 2048)], 'headerlines',skipRows);
fclose(fileID);

% # Extract data:

Time      = RawData{1}; 
Timestamp = RawData{2};
data = cell2mat(RawData(3:end));

% # Display data:
Time
Timestamp

Results - instead of 5 lines, there are 10 lines with every second line containing missing data:

Time = 
    '12:23:14.305'
    [1x1 char]
    '12:23:14.815'
    [1x1 char]
    '12:23:15.326'
    [1x1 char]
    '12:23:15.837'
    [1x1 char]
    '12:23:16.351'
    [1x1 char]

Timestamp =
   1.0e+12 *
    1.4357
       NaN
    1.4357
       NaN
    1.4357
       NaN
    1.4357
       NaN
    1.4357
       NaN

Is here an error in my code? How can I read the data correctly?


Solution

  • I've tested your code agaist the whole inout file and I'got the same error.

    As written by @excaza in its comment, the problem seems related to the way your input file has been writtent.

    You can fix the problem by adding a \r at the end of the format string as follows:

    RawData = textscan(fileID,['%s\t','%f\t',repmat('%f\t',1, 2048),'\r'], 'headerlines',skipRows);
    

    Notice: in the code you've posted, you wrote:

    data = cell2mat(Raw(3:end));
    

    I assume it was:

    data = cell2mat(RawData(3:end));
    

    Hope this helps.