How can I read ASCII delimited file, that its columns are seperated by more than one delimiter?

1 view (last 30 days)
Hello,
I'm trying to read the following text file (try1.txt) into a matrix:
0:0:14.0 27.7
0:0:15.0 27.7
0:0:16.0 27.3
0:0:17.0 27.5
0:0:18.0 27.6
0:0:19.0 27.6
I want that each number that is separated by ':' or '\t', will appear in a different column in the new matrix.
If I'm typing:
M=dlmread('try1.txt',':',1,0)
I get only half of the original data in my new matrix:
M =
0 0 14.0000
27.7000 0 0
0 0 15.0000
27.7000 0 0
0 0 16.0000
27.3000 0 0
Can someone help me to achieve a matrix M with 6 rows and 4 columns?
Thanks in advance.

Accepted Answer

Shalev
Shalev on 3 Jun 2011
Thanks for you both for the quick answers. A combination between the answers (below) gave me the best solution:
fid = fopen('5-29-2011_15-51.txt', 'r' );
a=textscan(fid,'%f:%f:%f %f' );
M=[a{1,1} a{1,2} a{1,3} a{1,4}];

More Answers (2)

Walter Roberson
Walter Roberson on 2 Jun 2011
Try using ':\t' as the delimiter.
  2 Comments
Shalev
Shalev on 2 Jun 2011
I tried, it doesn't work. I received the next error:
??? Error using ==> dlmread at 75
DELIMITER must be a single character.
Walter Roberson
Walter Roberson on 2 Jun 2011
You could use textscan: it accepts multiple delimiters.http://www.mathworks.com/help/techdoc/ref/textscan.html

Sign in to comment.


Robert Cumming
Robert Cumming on 2 Jun 2011
using low level commands you can read it as so:
fid = fopen ( 'temp.txt', 'r' );
myMat = zeros(6,4);
if fid ~= -1
for i=1:6
[myMat(i,1) myMat(i,2) myMat(i,3) myMat(i,4)] = strread ( fgetl ( fid ), '%f:%f:%f %f' );
end
fclose ( fid );
end
myMat
this makes an assumption that your datafile always contains 6x4 data.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!