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

조회 수: 24 (최근 30일)
Jay
Jay 2016년 9월 30일
댓글: Image Analyst 2016년 10월 2일
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)

채택된 답변

Jay
Jay 2016년 10월 2일
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
Image Analyst
Image Analyst 2016년 10월 2일
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.

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

추가 답변 (2개)

Mischa Kim
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
Mischa Kim
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)
Jay
Jay 2016년 10월 1일
편집: Jay 2016년 10월 1일
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

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


Image Analyst
Image Analyst 2016년 9월 30일
편집: Image Analyst 2016년 9월 30일
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
Image Analyst
Image Analyst 2016년 9월 30일
Calling fopen() does create the file first, then opens it for writing. Then fprintf() writes into it.
Jay
Jay 2016년 10월 1일
Thanks Image, that's what I thought.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by