How to print arrays of strings to the txt file with spaces and new lines?
7 次查看(过去 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{:}
0 个评论
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!