Why is the following code not working to print using fprintf ?
이전 댓글 표시
I am trying to print the following into the text file 'leftImageResults'.
Can someone please inform me as to why the code is not printing nor creating the desired text file?
I have used: https://au.mathworks.com/help/matlab/ref/fprintf.html#btf98f7 as a guide but still can't figure out why it is not working.
% Headers to use in text file output
headLeft1 = 'Left IO Values of Interest'
headLeft2 = 'Least Squares Adjustment Results of Interest'
headLeft3 = 'Parameter Corrections Vector'
headLeft4 = 'Residuals Vector'
headLeft5 = 'Transformed Points'
% LSA Results
strVal1 = [mat2str(A_left)] % A matrix Left
strVal2 = [mat2str(del_hatLeft)] % Corrections Vector
strVal3 = [mat2str(w_left)] % Misclosure Vector
strVal4 = [mat2str(r_hatLeft)] % Residuals
fid = fopen('LeftImageResults.txt','w')
fprintf(fid,'%1s\r\n %2s\r\n %3s\r\n %4s\r\n ',headLeft1,headLeft2,headLeft3,headLeft4)
fprintf(fid,'%1.6f\r\n %2.6f\r\n %3.6f\r\n %4.6f\r\n', strVal1, strVal2, strVal3, strVal4)
fclose(fid)
채택된 답변
추가 답변 (2개)
Mischa Kim
2016년 9월 30일
Justin, I assume all variables necessary to create your strVal variables are properly defined, e.g. A_left?
To start with you could comment out the four commands below
% LSA Results
and see if the txt file is created. This part should work.
댓글 수: 3
Jay
2016년 9월 30일
Mischa Kim
2016년 9월 30일
Can you attach the entire code?
What happens if you run:
% Headers to use in text file output
headLeft1 = 'Left IO Values of Interest'
headLeft2 = 'Least Squares Adjustment Results of Interest'
headLeft3 = 'Parameter Corrections Vector'
headLeft4 = 'Residuals Vector'
headLeft5 = 'Transformed Points'
% LSA Results
% strVal1 = [mat2str(A_left)] % A matrix Left
% strVal2 = [mat2str(del_hatLeft)] % Corrections Vector
% strVal3 = [mat2str(w_left)] % Misclosure Vector
% strVal4 = [mat2str(r_hatLeft)] % Residuals
fid = fopen('LeftImageResults.txt','w')
fprintf(fid,'%1s\r\n %2s\r\n %3s\r\n %4s\r\n ',headLeft1,headLeft2,headLeft3,headLeft4)
% fprintf(fid,'%1.6f\r\n %2.6f\r\n %3.6f\r\n %4.6f\r\n', strVal1, strVal2, strVal3, strVal4)
fclose(fid)
Image Analyst
2016년 9월 30일
편집: Image Analyst
2016년 9월 30일
0 개 추천
I don't have your data so I can't really reproduce. But try 'wt' instead of 'w' when you call fopen. And try using various combinations of \n, \n\r, and \r\n, until you get what you want.
And don't use %1s etc. Just use %s alone.
댓글 수: 3
Jay
2016년 9월 30일
Image Analyst
2016년 9월 30일
Calling fopen() does create the file first, then opens it for writing. Then fprintf() writes into it.
Jay
2016년 10월 1일
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!