fprintf row indentation export
8 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!