How to print arrays of strings to the txt file with spaces and new lines?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kapa Kudaibergenov
il 30 Ott 2019
Commentato: Kapa Kudaibergenov
il 4 Nov 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 Commenti
Risposta accettata
Mohammad Sami
il 1 Nov 2019
On windows machine you should use "\r\n" for new line and for tabs you can use "\t"
2 Commenti
Walter Roberson
il 1 Nov 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!