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!