필터 지우기
필터 지우기

Printing to text file

조회 수: 1 (최근 30일)
Edward Umpfenbach
Edward Umpfenbach 2012년 8월 25일
Pretty simple. I want to print this to a text file:
4
1
2
3
4
but instead I am getting this:
41234
Here's my code:
z = 4;
fid = fopen([mnl_data_path, 'packages.txt'], 'w');
a = num2str(z);
fprintf(fid,'%s\n',a);
for j = 1:1:z
a = num2str(j);
fprintf(fid,'%s\n',a);
end
fclose(fid);
Why isn't it printing to a new line?

답변 (4개)

the cyclist
the cyclist 2012년 8월 25일
Strange. I get each digit on a separate line, as you intend.
I am using the prerelease of R2012b, on a Mac running OSX 10.6.8.

Edward Umpfenbach
Edward Umpfenbach 2012년 8월 26일
Well I'm not sure what to do. It's definitely updating the file each time (if I change it to z = 5), I see "512345" printed to the text file.
2010a Windows 7

Edward Umpfenbach
Edward Umpfenbach 2012년 8월 26일
Nevermind. When I open with notepad I see "41234". When I open with notepad++, I see
4 1 2 3 4
So I guess it is not Matlab, it is notepad.
  댓글 수: 2
per isakson
per isakson 2012년 8월 26일
Did you try View | Show Symbol | Show All Characters in notepad++?
Image Analyst
Image Analyst 2012년 8월 27일
Try wordpad. I think wordpad and notepad behave differently with regards to how they interpret \n and \r\n. You can know for sure what's there if you have access to an editor that can view in hex format.

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


Walter Roberson
Walter Roberson 2012년 8월 27일
Open the file with 'wt' instead of 'w'

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by