フィルターのクリア

fprintf row indentation export

15 ビュー (過去 30 日間)
curoi
curoi 2013 年 9 月 7 日
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 件のコメント
dpb
dpb 2013 年 9 月 9 日
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 2013 年 9 月 9 日
The 9 would be the minimum width, and more width will be silently used if needed to satisfy the precision and format specifier.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2013 年 9 月 8 日
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 件のコメント
curoi
curoi 2013 年 9 月 10 日
編集済み: curoi 2013 年 9 月 10 日
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 2013 年 9 月 10 日
編集済み: curoi 2013 年 9 月 10 日
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'.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by