0
votes

I have a question regarding the importing of .txt files. The file is in the format below, the problem is that matlab does not seem to recognize the "new line" character indicators following every "$", so matlab just sees the 5th line as a continuous stream of data

Data Matlab sees:

01-24-2013 [6:01:53] 
Kp (0070.0000)
Ki  (0200.0000)
Kd (0009.0000)
$,0045,0044,0000.05,0011.53,0005.64,$,0045,0048,0000.04,0011.55,0005.66,$....etc
01-24-2013 [7:01:48]

Data Wordpad sees:

01-24-2013 [6:01:53] 
Kp (0070.0000)
Ki  (0200.0000)
Kd (0009.0000)
$,0045,0044,0000.05,0011.53,0005.64,
$,0045,0048,0000.04,0011.55,0005.66,
$, ....

I have no problem importing the format seen by "wordpad (re-saved with)" using "csvread" and skipping column 1, but for the raw .txt file "Data Matlab sees", I cant find a way to tell Matlab how to read. Ideally, I would like to tell Matlab to skip to Row-5, then start reading data and creating a new line in the matrix [nx5] every time it encounters a "$". Is there a way to detect the "$" and reformat the data into a usable matrix form?

Thanks!

1

1 Answers

1
votes

I don't know how you managed to read this data as one line, but suppose you did and you want to split it. You can use the almighty regexp to for that:

C = regexp(str, '\$,', 'split');

Then turn the strings into numbers and convert everything into a matrix:

C = cellfun(@str2num, C, 'Uniform', false);
A = vertcat(C{:});

Regarding the second part of the question:

Ideally, I would like to tell Matlab to skip to Row-5, then start reading data...

You can make textread do that by using the 'headerlines' option:

C = textread('file.txt', '%s', 1, 'headerlines', 4, 'delimiter', '\n')
str = C{1};

and then use the code that employs regexp to split the string str.

Note that this will only work if MATLAB indeed "sees" the 5th line like you described. If not, you'll simply get only the first row in your matrix.

Example

str = '$,0045,0044,0000.05,0011.53,0005.64,$,0045,0048,0000.04,0011.55,0005.66';
C = cellfun(@str2num, regexp(str, '\$,', 'split'), 'Uniform', false);
A = vertcat(C{:})

This results in:

A =
   45.0000   44.0000    0.0500   11.5300    5.6400
   45.0000   48.0000    0.0400   11.5500    5.6600