Borrar filtros
Borrar filtros

Write a 2D character array to textual file

10 visualizaciones (últimos 30 días)
Marko Gulin
Marko Gulin el 6 de Sept. de 2017
Editada: Marko Gulin el 6 de Sept. de 2017
Hy everyone,
What would be the best way in terms of computational time to write a 2D character array from Matlab to a textual file?
I've used this:
dlmwrite('file.txt', data, '');
but it is too slow.
I want to write a 2D character array so that each array row is printed on a new line.
Thanks!
  1 comentario
Stephen23
Stephen23 el 6 de Sept. de 2017
Probably the fastest way would be to use fprintf directly. Have you tried that?

Iniciar sesión para comentar.

Respuesta aceptada

Cam Salzberger
Cam Salzberger el 6 de Sept. de 2017
Editada: Cam Salzberger el 6 de Sept. de 2017
Hello Marko,
The most easily read code would probably be to either stick with dlmwrite, or loop through each row and use fprintf on an open file. However, if you want to avoid loops, you could do it like this:
fid = fopen('test.txt','w');
Anew = [A repmat(newline,size(A,1),1)];
fprintf(fid,'%s',Anew.');
fclose(fid);
Appending newlines on the ends of the rows makes it unnecessary to loop row by row. Transposing the array before writing it is necessary because fprintf works in linear indexing order (column-major).
-Cam
  2 comentarios
Stephen23
Stephen23 el 6 de Sept. de 2017
Editada: Stephen23 el 6 de Sept. de 2017
A slightly clearer way to add the newline:
Anew = A;
Anew(:,end+1) = newline;
Marko Gulin
Marko Gulin el 6 de Sept. de 2017
Editada: Marko Gulin el 6 de Sept. de 2017
Thank you, all of these answers have helped me :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings 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