fprintf row indentation export
显示 更早的评论
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 个评论
Walter Roberson
2013-9-8
How many columns are there in rough_cell ?
If you examine the file byte by byte, then what shows up at that point? For example,
T = fileread('q.txt');
T(1:300) %display as text
T(1:300) + 0 %as decimal
Image Analyst
2013-9-8
What's in the cells? Numbers or strings? It looks like strings (numbers that have been converted into string representations).
dpb
2013-9-8
I'd suggest also showing the code by which the array rough_cell was created--I'd guess the reason for it is therein.
curoi
2013-9-8
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!