1

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?

GegznaV
  • 4,938
  • 4
  • 23
  • 43
  • Using your example code and the sample data I obtain the expected output, not what you're receiving. I think it might have to do with how your lines are terminated. – sco1 Aug 13 '15 at 16:47
  • @excaza I've found a solution right while you were posting your comment. I've posted it as "Answer" but I can delete it if you think it was not fair. – il_raffa Aug 13 '15 at 17:01
  • @il_raffa not a big deal, appreciate the thought though – sco1 Aug 13 '15 at 17:13

1 Answers1

2

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.

il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • Thank you. It helped me to solve the problem. I thought, it should be `\n`, but it did not work. `\r` was what I needed. – GegznaV Aug 13 '15 at 19:50