필터 지우기
필터 지우기

How to combine sprintf and fprintf

조회 수: 1 (최근 30일)
TheBeginner
TheBeginner 2013년 1월 9일
Hello everyone,
Here's what I want to do :
-I use fprintf to print a sentence with new lines inside, for example:
text=sprintf('%s \n %s',sentence1,sentence2);
-Then I want to save it in a .txt file with this :
file=fopen(sprintf('%s%s%s',filepath,filename,'.txt'),'w');
fprintf(file,'%s',text);
It displays what I wrote except the new line : I have in my file sentence1sentence2
Any idea on how to fix this problem?
Thank you!
  댓글 수: 1
Jan
Jan 2013년 1월 9일
편집: Jan 2013년 1월 9일
A more stable method to create the file name due to the consideration of file separators:
fopen(fullfile(filepath, [filename, '.txt']))

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

답변 (1개)

Jan
Jan 2013년 1월 9일
편집: Jan 2013년 1월 9일
How do you test if there is no newline? You write the file in binary mode, such that CHAR(10) is used as line break. All editors I know can handle this correctly, but not the standard "Editor" of Windows. So please open the file in WordPad or Matlab's editor also.
I leave it up to others to mention the 'wt' method for fopen, because I usually warn for the eventually unexpected side-effects.
  댓글 수: 1
TheBeginner
TheBeginner 2013년 1월 9일
Ok, when I open it with WordPad I have the new line!
But I don't really understand why the editor of Windows canno't recognize that there's a new line.
Thank you!

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by