How can I read ASCII delimited file, that its columns are seperated by more than one delimiter?
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 2 de Jun. de 2011
0 votos
Try using ':\t' as the delimiter.
2 comentarios
Shalev
el 2 de Jun. de 2011
Walter Roberson
el 2 de Jun. de 2011
You could use textscan: it accepts multiple delimiters.http://www.mathworks.com/help/techdoc/ref/textscan.html
Robert Cumming
el 2 de Jun. de 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.
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!