\n Not Generating New Line In fprintf fid Command When Writing To .dat File
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a loop going and am trying to write output each time to a .dat text file appending it every iteration; but not getting a new line.
for minute = 18:1:48
thetaL = 15*(hour+minute/60+second/3600);
h = thetaL-alpha;
a = asin(sin(phi0)*sin(beta)+cos(phi0)*cos(beta)*cos(h));
A = atan((sin(h))/(cos(h)*sin(phi0)-tan(beta)*cos(phi0)));
fid = fopen('astro_conv_table.dat', 'a');
fprintf(fid, '%.0f %s %.0f %s %.0f %.0f %.0f
\n',hour,':',minute,':',second,a,A);
fclose(fid);
end
Everything in the code is working fine except the output is written to file in one long line as opposed to intended new line every iteration.
Thank you.
댓글 수: 0
채택된 답변
José-Luis
2012년 10월 4일
편집: José-Luis
2012년 10월 4일
I assume you are in Windows. The return of carriage for Windows is "\r\n". Try changing that. Otherwise, use wordpad instead of notepad. Or get any decent text editor, like notepad++, which is free.
댓글 수: 4
Jan
2012년 10월 4일
Even on Windows machines "\n" is a newline. Only the "Editor" of Windows does not understand this correctly. I do not know any other software, which does not accept the CHAR(10) as line break properly.
Matlab's LOAD does not recognize the CHAR(13) line break when reading a file in ASCII format, but this has been used by old Apple software until OS-9 only.
José-Luis
2012년 10월 4일
Well, that is news to me. I guess things have changed:
추가 답변 (1개)
Stephen23
2018년 1월 12일
The simplest solution by far is to fopen the file in text mode, simply by using the t option. This mode converts all newlines to \n when reading, and when writing converts \n to the appropriate type for your OS.
For the original question, all that is required is adding the t to fopen:
fid = fopen('astro_conv_table.dat', 'at'); % note the "t" !
fprintf(fid, '%.0f %s %.0f %s %.0f %.0f %.0f\n',hour,':',minute,':',second,a,A);
fclose(fid);
and it will insert the correct Windows newline characters.
댓글 수: 1
Jan
2018년 1월 12일
While opening in text mode is simple, it is also simple to stay at char(10) on all machines and avoid to use the Windows Notepad. I prefer the 2nd way of simplicity, most of all if I want to discuss with a Linux or Mac user.
참고 항목
카테고리
Help Center 및 File Exchange에서 Standard File Formats에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!