How can I read ASCII delimited file, that its columns are seperated by more than one delimiter?
이전 댓글 표시
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.
채택된 답변
추가 답변 (2개)
Walter Roberson
2011년 6월 2일
0 개 추천
Try using ':\t' as the delimiter.
댓글 수: 2
Shalev
2011년 6월 2일
Walter Roberson
2011년 6월 2일
You could use textscan: it accepts multiple delimiters.http://www.mathworks.com/help/techdoc/ref/textscan.html
Robert Cumming
2011년 6월 2일
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.
카테고리
도움말 센터 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!