fprintf row indentation export
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 9 de Sept. de 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
el 9 de Sept. de 2013
The 9 would be the minimum width, and more width will be silently used if needed to satisfy the precision and format specifier.
Respuesta aceptada
Walter Roberson
el 8 de Sept. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!