How can I read ASCII delimited file, that its columns are seperated by more than one delimiter?
Show older comments
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
More Answers (2)
Walter Roberson
on 2 Jun 2011
0 votes
Try using ':\t' as the delimiter.
2 Comments
Shalev
on 2 Jun 2011
Walter Roberson
on 2 Jun 2011
You could use textscan: it accepts multiple delimiters.http://www.mathworks.com/help/techdoc/ref/textscan.html
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.
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!