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!