How to print arrays of strings to the txt file with spaces and new lines?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kapa Kudaibergenov
el 30 de Oct. de 2019
Comentada: Kapa Kudaibergenov
el 4 de Nov. de 2019
I have a problem with printing an array of strings, which I'd like to print to a txt file. The problem is I can't put spaces between elements. My new lines don't work neither.
Here's a snipped of my code:
out3 = fullfile('D:\new.txt');
fid1 = fopen(out3,'a');
for i = 1:Numb_of_folds
pattern = nameFolds1(i);
TF = startsWith(files,pattern);
Z = files(TF);
Z = cellstr(Z.');
fprintf(fid1,'\n');
fprintf(fid1,'%s %s\n\r %s',Z{:});
fprintf(fid1,'\n');
%fprintf(fid1,'%s\n\r',pattern);
%fclose(fid1);
end
fclose(fid1);
winopen(out3)
'Numb_of_folds' - number of times I need to begin from the new line.
The 'files' variable is array of strings which I need to print.
I am checking if some of them begins with the specified pattern, we print on one line.
The problem is it prints it like :
DYT01_OFF DYT02_OFF DYT03_OFFDYT04_OFF DYT06_OFF DYT07_OFFDYT13_OFF
These two strings I use, but I don't know why. I just read that I need this.
Z = cellstr(Z.');
Z{:}
0 comentarios
Respuesta aceptada
Mohammad Sami
el 1 de Nov. de 2019
On windows machine you should use "\r\n" for new line and for tabs you can use "\t"
2 comentarios
Walter Roberson
el 1 de Nov. de 2019
Alternately, instead of opening
fid1 = fopen(out3,'a');
you can
fid1 = fopen(out3,'at');
If you do that, then on WIndows machines, each \n you output will be automatically translated to \r\n
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings 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!