Next line command in fprintf is not working while writing in text file

조회 수: 4 (최근 30일)
Sandeep Nair
Sandeep Nair 2019년 1월 24일
편집: Jan 2019년 1월 24일
Am trying to generate a script so there is a specific format which i need to get so that i could use the file generated in other software for getting results
My issue is
Suppose
My script is like
$...........Model
Test='0'
test1='good'
Am trying to use a code like
format=[' $...........Model \n Test=''0'' \n test1=''good'''];
fileid=fopen('test.txt','w')
fprintf(fileid,format)
fclose(fileid)
But my results are coming as $...........Model Test=''0'' test1=''good
It is not going to the next line
Could you please help me how to get it in next line .txt format
  댓글 수: 2
Stephen23
Stephen23 2019년 1월 24일
format = [' $...........Model \n Test=''0'' \n test1=''good'''];
The square brackets on that line are completely superfluous. Get rid of them.
Sandeep Nair
Sandeep Nair 2019년 1월 24일
편집: Sandeep Nair 2019년 1월 24일
Dear Stephen;
But here the code which i used is short the exact script is very big so am using lthe square brackets,if i don't use square brackets it will give an error message
How my script looks like
format = ['................................Model \n' ...
'Test =''0'' \n ...
'test1=''good'''];
If i try to avoid the square brackets the ... three dots its not taking hence using square brackets

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2019년 1월 24일
편집: Jan 2019년 1월 24일
The shown code is working and correctly writing a line break:
Test = '0'
test1 = 'good'
Fmt = ' $...........Model \n Test=''%s'' \n test1=''%s''';
fileid = fopen('test.txt', 'w')
fprintf(fileid, Fmt, Test, test1);
fclose(fileid)
Try it. If you do not see linebreaks, you either use an old version of MS Notepad - all other editors display char(10) correctly. Or you do not look at the created file, because the current folder differs from your expectations. Use absolute path names instead:
fileid = fopen(fullfile(tempdir, 'test.txt'), 'w')
if fileid == -1 % Check success in every case!
error('Cannot open file for writing');
end

추가 답변 (1개)

Stephen23
Stephen23 2019년 1월 24일
편집: Stephen23 2019년 1월 24일
fileid = fopen('test.txt','wt')
^ open as a text file.
And also stop using awful, outdated, featureless, retrograde MS Notepad to view text files.
  댓글 수: 1
Jan
Jan 2019년 1월 24일
MS Notepad handles CHAR(10) correctly in modern Windows 10 systems. Therefore I do not know any software anymore, which requires the char([13,10]) of the text mode.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by