这是一个非常基本的问题,但是由于我是Matlab的新手,所以我一直在努力寻找一种做到这一点的好方法。我只想将一些串联的字符串打印到屏幕和文本文件中。Matlab正在“吃” \ n!
str1 = sprintf('Line 1\n');
str2 = sprintf('Line 2\n');
finalStr = strcat(str1,str2);
% Print on screen
fprintf('%s',finalStr );
% Result: Line 1Line 2. What happened to the \n ?? !!!!
% Print on file
[curPath,name,ext] = fileparts(mfilename('fullpath'));
infoPath = fullfile(curPath,'MyFile.txt');
fid = fopen(infoPath,'w'); % Write only, overwrite if exists
fprintf(fid,finalStr);
fclose(fid);
我还需要将finalStr保存到文本文件。我在这里想念的是什么?
函数strcat忽略空格。为了执行此操作,请使用:
finalStr = [str1, str2];
fprintf('%s',finalStr );
结果:
Line 1
Line 2
编辑:以“记事本”方式在文本文件中写入文本:
% Notepad needs \r also.
newline = sprintf('\n');
newlineNotepad = sprintf('\r\n');
strB = strrep(strA, newline, newlineNotepad);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句