To read a .dat file by replacing 'nil' s with zeros

2 visualizaciones (últimos 30 días)
aneps
aneps el 19 de Abr. de 2016
Comentada: Walter Roberson el 21 de Abr. de 2016
I have a .dat file with 3 columns :
56 1221.9522403147 4709.4286506579
57 1296.7593276229 2382.7334294788
58 1298.014307685 5378.3218155863
59 1672.5745510151 3586.6821218019
60 1293.7368062958 2311.6447578082
61 1066.5874471792 3652.8767627687
62 nil 7015.2811482462
63 1295.6502693748 12997.366157104
64 1473.1366368752 3878.5975654487
65 1893.552884693 4782.6543016909
66 1601.2373040587 5157.4325835724
This is a part of my data. it has some 'nil' s appearing somewhere. How can I read the data into a 3 x n matrix by replacing those 'nil' s with zeros?

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Abr. de 2016
fid = fopen('YourFile.dat', 'rt');
yourdata = cell2mat( textscan(fid, '%f%f%f', 'Delimiter', '\t', 'TreatAsEmpty', 'nil', 'EmptyValue', 0, 'CollectOutput', 1) );
fclose(fid);
  8 comentarios
aneps
aneps el 21 de Abr. de 2016
Yes, it worked.. thank you... I have one more question, if I have several data like Result_1.da, Result_2.dat.... Result_n.dat, how can I loop it to read all as yourdata1, yourdata2, .... yourdatan?

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 19 de Abr. de 2016
fid=fopen('file.dat')
v=textscan(fid,'%s')
fclose(fid)
w=reshape(v{:},3,[])'
w=strrep(w,'nil','0')
out=str2double(w)
  3 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 19 de Abr. de 2016
Use
n=5
v=textscan(fid,'%s','headerlines',n)
aneps
aneps el 21 de Abr. de 2016
did not work:( .. getting error message "Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier."

Iniciar sesión para comentar.

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by