Why is the following code not working to print using fprintf ?
Show older comments
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)
Accepted Answer
More Answers (2)
Mischa Kim
on 30 Sep 2016
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 Comments
Jay
on 30 Sep 2016
Mischa Kim
on 30 Sep 2016
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
on 30 Sep 2016
Edited: Image Analyst
on 30 Sep 2016
0 votes
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 Comments
Jay
on 30 Sep 2016
Image Analyst
on 30 Sep 2016
Calling fopen() does create the file first, then opens it for writing. Then fprintf() writes into it.
Jay
on 1 Oct 2016
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!