fprintf row indentation export

7 views (last 30 days)
curoi
curoi on 7 Sep 2013
I'm having trouble correctly exporting a custom text file I need. When I use fprintf within a for loop, I am getting strange formatting issues with spacing occuring before the beginning of every 5th row starting with row 6.
fileout = 'q.txt';
fid2 = fopen( fileout, 'w' );
for row = 1:5:n;
fprintf( fid2, ' %s %s %s %s\r\n', rough_cell{ row, : } );
fprintf( fid2, ' %s %s %s %s\r\n', rough_cell{ row+1, : } );
fprintf( fid2, ' %s %s %s %s\r\n', rough_cell{ row+2, : } );
fprintf( fid2, ' %s %s %s %s\r\n', rough_cell{ row+3, : } );
fprintf( fid2, ' %s %s %s\r\n', rough_cell{ row+4, : } );
end
fclose( fid2 );
Here is a sample of what the output text file looks like. Every 5th row aside from the 1st row is indented by about 8 spaces. I cannot figure out why my code is doing this. If I eliminate the last, shorter fprintf line within the for loop, I don't have this problem. However, I need to have every fifth row in output as well as the remaining alternating first four rows. As can be seen, there is no additional spacing in the formatSpec (just 1 space). I've also checked the rough_cell cell array and there is no additional spacing for the first column of every fifth row.
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
Any ideas?
  6 Comments
dpb
dpb on 9 Sep 2013
num2str( rough.blk, '%9.7E')
I didn't dig thru it all entirely, but it's possible the ill-formed format string could possibly contribute to the problem.
An E format w/ a precision of 7 digits needs a minimum of 14 spaces for the field accounting for the sign place, a leading digit, the decimal and the four for the exponent. Not sure what C (hence Matlab) does when there isn't enough room...
Walter Roberson
Walter Roberson on 9 Sep 2013
The 9 would be the minimum width, and more width will be silently used if needed to satisfy the precision and format specifier.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Sep 2013
Change
fid2 = fopen( fileout, 'w' );
to
fid2 = fopen( fileout, 'wt' );
and remove the \r from each of the format strings.
Note: how it shows up will depend on which method you use to output it. Not all MS Windows operations know that \r\n is to be treated together as a single newline.
  7 Comments
curoi
curoi on 10 Sep 2013
Edited: curoi on 10 Sep 2013
I went back and modified the T(1:1350) output above because there were apparently still issues with copying into the message board. You'll notice that there really is an extra space (3) between a positive value and the preceding string (The 0.040 doesn't line up but the 0.024 does). In addition, there are only 6 spaces in front of the beginning of offending sixth set instead of 8 now. Due to the message board, all 2 spaces at the beginning of each line are dropped out but do actually appear in my text and T(1:1350) + 0 output above.
Anyway, I went back to check the rough_cell cell array and at the end of every sixth line there are 5 blank cells which is why the preceding line you mentioned is short by 5 entries out of 12. I did this knowing that I only needed 7 values in the last line and I believed I could just specify that in the fprintf command separately from the other sets (lines) of strings. If I can't do that, should I just fill each of those missing cells with some arbitrary number of the same size and keep my fprintf specification at only 7 strings?
curoi
curoi on 10 Sep 2013
Edited: curoi on 10 Sep 2013
I figured out the problem. It was just that in my fprintf, I was specifying all 12 rows for the short 7 string line by using ':' instead of '1:7'.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings 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!