tab delimited .txt file

209 visualizaciones (últimos 30 días)
ricco
ricco el 18 de Nov. de 2011
Comentada: Sang Min Kim el 11 de Nov. de 2024 a las 19:18
In order to write a matrix as a tab delimited .txt file I can use the following command:
dlmwrite(Filename, Data, 'delimiter', '\t');
Say that the dimensions of 'Data' is [m n], how is it possible to include a non numeric vector (i.e. vector containing words) as m(1) in order to state what each colummn refers to in the .txt file?
thanks

Respuesta aceptada

Grzegorz Knor
Grzegorz Knor el 18 de Nov. de 2011
Suppose that your Data is:
Data = [1 2 3;4 5 6; 7 8 9];
Would you like to create such a file?
line1 1 2 3
line2 4 5 6
line3 7 8 9
If so, this is my suggestion:
Data = [1 2 3;4 5 6; 7 8 9];
names = {'line1','line2','line3'};
fid = fopen('test_file','w');
for k=1:3
fprintf(fid,'%s\t%.2f\t%.2f\t%.2f\n',names{k},Data(k,:));
end
fclose(fid);
  4 comentarios
ricco
ricco el 19 de Nov. de 2011
thanks that works perfectly. How would this code be altered if I needed the format of the .txt file to have the header on the top of each column of the matrix not on each row?
Paul Safier
Paul Safier el 27 de Mzo. de 2019
What if the data matrix had more than a few columns, say thousands? Would you have to type a format (e.g. .2f\t%) for each column?

Iniciar sesión para comentar.

Más respuestas (2)

Adam Danz
Adam Danz el 17 de Jun. de 2020
For more recent releases of Matlab (r2013b or later), use writetable() with the delimiter set to \t.
writetable(T,'tabledata2.txt','Delimiter','\t')
  1 comentario
Sang Min Kim
Sang Min Kim el 11 de Nov. de 2024 a las 19:18
Excellent, very easy to use!

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 18 de Nov. de 2011
dlmwrite() can only be used for numeric data or for pure character data (an ugly hack with little purpose). dlmwrite() cannot be used for mixing character data and numeric data.
Grzegorz's proposed code looks fine.

Categorías

Más información sobre Standard File Formats 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