Paste Strings to Text File
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Kevin
el 25 de Jun. de 2014
Comentada: José-Luis
el 26 de Jun. de 2014
I need help performing what I believe to be a relatively simply task:
I have code that uses a loop to generate a string (of variable length) with each loop iteration. I want to paste all the generated strings to a text file, with each string on its own line. The strings should be ordered in the text file by the order in which they are generated by the loop.
Can anyone offer help?
Thanks.
1 comentario
José-Luis
el 25 de Jun. de 2014
Editada: José-Luis
el 25 de Jun. de 2014
How about storing all the strings in a cell array and saving all at once? You could also append to a file each time that a string is created but that is bound to be slower.
Some functions you might want to use:
doc fopen
doc fprintf
Respuesta aceptada
José-Luis
el 25 de Jun. de 2014
your_text = cell(10,1);
for ii = 1:10
your_text(ii) = {'Lorem ipsum'};
end
your_text(10) = {'No!'};
fid = fopen('test.txt','w');
fprintf(fid,'%s\n',your_text{:});
fclose(fid)
3 comentarios
José-Luis
el 26 de Jun. de 2014
You must be using Windows:
fprintf(fid,'%s\r\n',your_text{:});
Más respuestas (0)
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!