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

8 次查看(过去 30 天)
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{:}

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by