Why is the following code not working to print using fprintf ?

29 views (last 30 days)
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

Jay
Jay on 2 Oct 2016
I found the problem.
As I am working of a portable HD I would select the drive and select the "Add to Path" then "Selected Folders and Subfolders" option rather than specify the specific file path.
The txt file was created and added to the bottom of the complete folders and files list in the drive (lost amongst the numerous other files).
Thanks for all your help Mischa and Image Analyst
  1 Comment
Image Analyst
Image Analyst on 2 Oct 2016
Well we never would have guessed that (since you never mentioned setting the path before).
I still don't know what you did with "Add to Path" then "Selected Folders and Subfolders" option. There is a "Set Path" button on the Home tab of the tool ribbon, and after you click that a "Add with Subfolders" button, but it requires you to specify a specific path, which you say you did not do, and it adds it to the top/beginning of the path, not the bottom. But that's all different than what you did.
But whatever...at least it's working.

Sign in to comment.

More Answers (2)

Mischa Kim
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
Mischa Kim
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)
Jay
Jay on 1 Oct 2016
Edited: Jay on 1 Oct 2016
Running the following code:
% 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','wt')
fprintf(fid,'%s %s %s %s\n ',headLeft1,headLeft2,headLeft3,headLeft4)
fprintf(fid,'%.6f %.6f %.6f %.6f\n', strVal1, strVal2, strVal3, strVal4)
fclose(fid)
The command window outputs:
headLeft1 =
Left IO Values of Interest
headLeft2 =
Least Squares Adjustment Results of Interest
headLeft3 =
Parameter Corrections Vector
headLeft4 =
Residuals Vector
headLeft5 =
Transformed Points
strVal1 =
[3482.055 -280.3875 1 0;-280.3875 -3482.055 0 1;3468.405 -3497.265 1 0;-3497.265 -3468.405 0 1;254.145 -3481.535 1 0;-3481.535 -254.145 0 1;268.4725 -267.7825 1 0;-267.7825 -268.4725 0 1;1867.32 -1881.635 1 0;-1881.635 -1867.32 0 1]
strVal2 =
[0.0140001478661937;-6.13134643096367e-05;-26.3329640591793;26.069033817171]
strVal3 =
[-22.431;-22.341;-22.448;22.668;22.572;22.659;22.564;-22.341;0.064;0.159]
strVal4 =
[0.00251234801378786;0.0160642123053307;-0.00835176657502856;-0.0125333839139401;0.0105684922396989;0.00161152622938943;0.00610931158383465;-0.00449979976112047;-0.0108383852623071;-0.000642554859647843]
fid =
3
ans =
119
ans =
5841
ans =
0

Sign in to comment.


Image Analyst
Image Analyst on 30 Sep 2016
Edited: Image Analyst on 30 Sep 2016
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
Image Analyst
Image Analyst on 30 Sep 2016
Calling fopen() does create the file first, then opens it for writing. Then fprintf() writes into it.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!