Printing heading to text file using fprintf

조회 수: 2 (최근 30일)
Orongo
Orongo 2017년 7월 2일
댓글: Star Strider 2017년 7월 2일
Hi, I am amending my text file with headings using the function fprintf. I took inspiration from other forums and made this script
load D
age=1:8;
year=1990:1:1997;
D_w_hdg=[year' age' D];
save('D.txt','D','-ascii')
fileID = fopen('D.txt','w');
fprintf(fileID,'%4s %3s %4s %4s %4s\n','year', 'age', 'male', 'male', 'male');
fprintf(fileID,'%4u %2u %2.6d %2.6d %2.6d\n',D_w_hdg);
fclose(fileID);
type 'D.txt'
The result in the Command window looks ok, but when I open the D.txt in Notepad the result is all in one line. I want the result in Notepad to reflect what is shown with the command type 'D.txt'. Is this possible?

채택된 답변

Star Strider
Star Strider 2017년 7월 2일
I do not have ‘D’, so I cannot run your code.
Try transposing your matrix:
fprintf(fileID,'%4u %2u %2.6d %2.6d %2.6d\n',D_w_hdg');
  댓글 수: 4
Orongo
Orongo 2017년 7월 2일
I see I was opening it in notes before. Opening it in Notepad++ shows the desired results. Strange, but it works now. Thanks!
Star Strider
Star Strider 2017년 7월 2일
My pleasure!
Did you see the solution to your earlier Question that I posted?

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by