Filter löschen
Filter löschen

How to print arrays of strings to the txt file with spaces and new lines?

8 Ansichten (letzte 30 Tage)
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{:}

Akzeptierte Antwort

Mohammad Sami
Mohammad Sami am 1 Nov. 2019
On windows machine you should use "\r\n" for new line and for tabs you can use "\t"
  2 Kommentare
Walter Roberson
Walter Roberson am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by