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 个评论

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
What's in the cells? Numbers or strings? It looks like strings (numbers that have been converted into string representations).
I'd suggest also showing the code by which the array rough_cell was created--I'd guess the reason for it is therein.
Walter, there are actually 12 columns in rough_cell but I shortened the number of strings in the for loop code so the output would fit in the message. Here is the actual display text:
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.99000
Image Analyst, the cells are composed of strings not numbers. dpb, here is the preceding code and the real 12 column code (unshortened):
for i = 1:12;
depth( 1:n, i) = C1{ 1, i };
end
[ indx.nan( :, 1 ), indx.nan( :, 2 ) ] = find( ~isnan( depth ) == 0 );
[ indx.blk( :, 1 ), indx.blk( :, 2 ) ] = find( depth == -999 );
[ indx.hgr( :, 1 ), indx.hgr( :, 2 ) ] = find( -998 <= depth ...
& depth <= -10 );
[ indx.lwr( :, 1 ), indx.lwr( :, 2 ) ] = find( -10 < depth ...
& depth <= -1 );
[ indx.fld( :, 1 ), indx.fld( :, 2 ) ] = find( -1 < depth ...
& depth <= 3 );
[ indx.chl( :, 1 ), indx.chl( :, 2 ) ] = find( 3 < depth ...
& depth <= 20 );
[ indx.sea( :, 1 ), indx.sea( :, 2 ) ] = find( 20 < depth ...
& depth < 100 );
for j = 1:length( indx.nan );
rough( indx.nan( j, 1 ), indx.nan( j, 2 ) ) = NaN;
end
for k = 1:length( indx.blk );
rough.blk = -999;
rough_str( 1, 1:15 ) = [ ' ', num2str( rough.blk, '%9.7E' ) ];
rough_cell( indx.blk( k, 1 ), indx.blk( k, 2 ) ) = cellstr( rough_str );
end
for m = 1:length( indx.hgr );
rough.hgr = 0.050;
rough_str( 1, 1:15 ) = [ ' ', num2str( rough.hgr, '%9.7E' ) ];
rough_cell( indx.hgr( m, 1 ), indx.hgr( m, 2 ) ) = cellstr( rough_str );
end
for p = 1:length( indx.lwr );
rough.lwr = 0.040;
rough_str( 1, 1:15 ) = [ ' ', num2str( rough.lwr, '%9.7E' ) ];
rough_cell( indx.lwr( p, 1 ), indx.lwr( p, 2 ) ) = cellstr( rough_str );
end
for q = 1:length( indx.fld );
rough.fld = 0.030;
rough_str( 1, 1:15 ) = [ ' ', num2str( rough.fld, '%9.7E' ) ];
rough_cell( indx.fld( q, 1 ), indx.fld( q, 2 ) ) = cellstr( rough_str );
end
for r = 1:length( indx.chl );
rough.chl = 0.024;
rough_str( 1, 1:15 ) = [ ' ', num2str( rough.chl, '%9.7E' ) ];
rough_cell( indx.chl( r, 1 ), indx.chl( r, 2 ) ) = cellstr( rough_str );
end
for t = 1:length( indx.sea );
rough.sea = 0.020;
rough_str( 1, 1:15 ) = [ ' ', num2str( rough.sea, '%9.7E' ) ];
rough_cell( indx.sea( t, 1 ), indx.sea( t, 2 ) ) = cellstr( rough_str );
end
fileout = 'q.txt';
fid2 = fopen( fileout, 'w' );
for row = 1:5:n;
fprintf( fid2, ' %s %s %s %s %s %s %s %s %s %s %s %s\r\n', rough_cell{ row, : } );
fprintf( fid2, ' %s %s %s %s %s %s %s %s %s %s %s %s\r\n', rough_cell{ row+1, : } );
fprintf( fid2, ' %s %s %s %s %s %s %s %s %s %s %s %s\r\n', rough_cell{ row+2, : } );
fprintf( fid2, ' %s %s %s %s %s %s %s %s %s %s %s %s\r\n', rough_cell{ row+3, : } );
fprintf( fid2, ' %s %s %s %s %s %s %s\r\n', rough_cell{ row+4, : } );
end
fclose( fid1 );
fclose( fid2 );
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...
The 9 would be the minimum width, and more width will be silently used if needed to satisfy the precision and format specifier.

请先登录,再进行评论。

 采纳的回答

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 个评论

I changed the file permission type to 'wt' and removed all \r from the format spec of each fprintf line in the for loop but I'm still getting 8 spaces in front of the 6, 11, 16, 21, etc lines.
Please show the output of
T = fileread('q.txt');
T(1:1350) %display as text
T(1:1350) + 0 %as decimal
Also, how are you viewing the file when you see the spaces?
>> T = fileread('q.txt');
T(1:1350) %display as text
T(1:1350) + 0 %as decimal
ans =
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 2.4000000E-02 4.0000000E-02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
-9.9900000E+02 -9.9900000E+02 -9.9900000E+02 -9.9900000E+02
ans =
Columns 1 through 12
32 32 45 57 46 57 57 48 48 48 48 48
Columns 13 through 24
69 43 48 50 32 32 45 57 46 57 57 48
Columns 25 through 36
48 48 48 48 69 43 48 50 32 32 45 57
Columns 37 through 48
46 57 57 48 48 48 48 48 69 43 48 50
Columns 49 through 60
32 32 45 57 46 57 57 48 48 48 48 48
Columns 61 through 72
69 43 48 50 32 32 45 57 46 57 57 48
Columns 73 through 84
48 48 48 48 69 43 48 50 32 32 45 57
Columns 85 through 96
46 57 57 48 48 48 48 48 69 43 48 50
Columns 97 through 108
32 32 45 57 46 57 57 48 48 48 48 48
Columns 109 through 120
69 43 48 50 32 32 45 57 46 57 57 48
Columns 121 through 132
48 48 48 48 69 43 48 50 32 32 45 57
Columns 133 through 144
46 57 57 48 48 48 48 48 69 43 48 50
Columns 145 through 156
32 32 45 57 46 57 57 48 48 48 48 48
Columns 157 through 168
69 43 48 50 32 32 45 57 46 57 57 48
Columns 169 through 180
48 48 48 48 69 43 48 50 32 32 45 57
Columns 181 through 192
46 57 57 48 48 48 48 48 69 43 48 50
Columns 193 through 204
13 10 32 32 45 57 46 57 57 48 48 48
Columns 205 through 216
48 48 69 43 48 50 32 32 45 57 46 57
Columns 217 through 228
57 48 48 48 48 48 69 43 48 50 32 32
Columns 229 through 240
45 57 46 57 57 48 48 48 48 48 69 43
Columns 241 through 252
48 50 32 32 45 57 46 57 57 48 48 48
Columns 253 through 264
48 48 69 43 48 50 32 32 45 57 46 57
Columns 265 through 276
57 48 48 48 48 48 69 43 48 50 32 32
Columns 277 through 288
45 57 46 57 57 48 48 48 48 48 69 43
Columns 289 through 300
48 50 32 32 45 57 46 57 57 48 48 48
Columns 301 through 312
48 48 69 43 48 50 32 32 45 57 46 57
Columns 313 through 324
57 48 48 48 48 48 69 43 48 50 32 32
Columns 325 through 336
45 57 46 57 57 48 48 48 48 48 69 43
Columns 337 through 348
48 50 32 32 45 57 46 57 57 48 48 48
Columns 349 through 360
48 48 69 43 48 50 32 32 45 57 46 57
Columns 361 through 372
57 48 48 48 48 48 69 43 48 50 32 32
Columns 373 through 384
45 57 46 57 57 48 48 48 48 48 69 43
Columns 385 through 396
48 50 13 10 32 32 45 57 46 57 57 48
Columns 397 through 408
48 48 48 48 69 43 48 50 32 32 45 57
Columns 409 through 420
46 57 57 48 48 48 48 48 69 43 48 50
Columns 421 through 432
32 32 45 57 46 57 57 48 48 48 48 48
Columns 433 through 444
69 43 48 50 32 32 45 57 46 57 57 48
Columns 445 through 456
48 48 48 48 69 43 48 50 32 32 45 57
Columns 457 through 468
46 57 57 48 48 48 48 48 69 43 48 50
Columns 469 through 480
32 32 45 57 46 57 57 48 48 48 48 48
Columns 481 through 492
69 43 48 50 32 32 45 57 46 57 57 48
Columns 493 through 504
48 48 48 48 69 43 48 50 32 32 45 57
Columns 505 through 516
46 57 57 48 48 48 48 48 69 43 48 50
Columns 517 through 528
32 32 45 57 46 57 57 48 48 48 48 48
Columns 529 through 540
69 43 48 50 32 32 45 57 46 57 57 48
Columns 541 through 552
48 48 48 48 69 43 48 50 32 32 45 57
Columns 553 through 564
46 57 57 48 48 48 48 48 69 43 48 50
Columns 565 through 576
32 32 45 57 46 57 57 48 48 48 48 48
Columns 577 through 588
69 43 48 50 13 10 32 32 45 57 46 57
Columns 589 through 600
57 48 48 48 48 48 69 43 48 50 32 32
Columns 601 through 612
45 57 46 57 57 48 48 48 48 48 69 43
Columns 613 through 624
48 50 32 32 45 57 46 57 57 48 48 48
Columns 625 through 636
48 48 69 43 48 50 32 32 45 57 46 57
Columns 637 through 648
57 48 48 48 48 48 69 43 48 50 32 32
Columns 649 through 660
45 57 46 57 57 48 48 48 48 48 69 43
Columns 661 through 672
48 50 32 32 45 57 46 57 57 48 48 48
Columns 673 through 684
48 48 69 43 48 50 32 32 45 57 46 57
Columns 685 through 696
57 48 48 48 48 48 69 43 48 50 32 32
Columns 697 through 708
45 57 46 57 57 48 48 48 48 48 69 43
Columns 709 through 720
48 50 32 32 45 57 46 57 57 48 48 48
Columns 721 through 732
48 48 69 43 48 50 32 32 45 57 46 57
Columns 733 through 744
57 48 48 48 48 48 69 43 48 50 32 32
Columns 745 through 756
45 57 46 57 57 48 48 48 48 48 69 43
Columns 757 through 768
48 50 32 32 45 57 46 57 57 48 48 48
Columns 769 through 780
48 48 69 43 48 50 13 10 32 32 45 57
Columns 781 through 792
46 57 57 48 48 48 48 48 69 43 48 50
Columns 793 through 804
32 32 45 57 46 57 57 48 48 48 48 48
Columns 805 through 816
69 43 48 50 32 32 45 57 46 57 57 48
Columns 817 through 828
48 48 48 48 69 43 48 50 32 32 45 57
Columns 829 through 840
46 57 57 48 48 48 48 48 69 43 48 50
Columns 841 through 852
32 32 32 50 46 52 48 48 48 48 48 48
Columns 853 through 864
69 45 48 50 32 32 32 52 46 48 48 48
Columns 865 through 876
48 48 48 48 69 45 48 50 32 32 45 57
Columns 877 through 888
46 57 57 48 48 48 48 48 69 43 48 50
Columns 889 through 900
13 10 32 32 32 32 32 32 32 32 45 57
Columns 901 through 912
46 57 57 48 48 48 48 48 69 43 48 50
Columns 913 through 924
32 32 45 57 46 57 57 48 48 48 48 48
Columns 925 through 936
69 43 48 50 32 32 45 57 46 57 57 48
Columns 937 through 948
48 48 48 48 69 43 48 50 32 32 45 57
Columns 949 through 960
46 57 57 48 48 48 48 48 69 43 48 50
Columns 961 through 972
32 32 45 57 46 57 57 48 48 48 48 48
Columns 973 through 984
69 43 48 50 32 32 45 57 46 57 57 48
Columns 985 through 996
48 48 48 48 69 43 48 50 32 32 45 57
Columns 997 through 1008
46 57 57 48 48 48 48 48 69 43 48 50
Columns 1009 through 1020
32 32 45 57 46 57 57 48 48 48 48 48
Columns 1021 through 1032
69 43 48 50 32 32 45 57 46 57 57 48
Columns 1033 through 1044
48 48 48 48 69 43 48 50 32 32 45 57
Columns 1045 through 1056
46 57 57 48 48 48 48 48 69 43 48 50
Columns 1057 through 1068
32 32 45 57 46 57 57 48 48 48 48 48
Columns 1069 through 1080
69 43 48 50 32 32 45 57 46 57 57 48
Columns 1081 through 1092
48 48 48 48 69 43 48 50 13 10 32 32
Columns 1093 through 1104
45 57 46 57 57 48 48 48 48 48 69 43
Columns 1105 through 1116
48 50 32 32 45 57 46 57 57 48 48 48
Columns 1117 through 1128
48 48 69 43 48 50 32 32 45 57 46 57
Columns 1129 through 1140
57 48 48 48 48 48 69 43 48 50 32 32
Columns 1141 through 1152
45 57 46 57 57 48 48 48 48 48 69 43
Columns 1153 through 1164
48 50 32 32 45 57 46 57 57 48 48 48
Columns 1165 through 1176
48 48 69 43 48 50 32 32 45 57 46 57
Columns 1177 through 1188
57 48 48 48 48 48 69 43 48 50 32 32
Columns 1189 through 1200
45 57 46 57 57 48 48 48 48 48 69 43
Columns 1201 through 1212
48 50 32 32 45 57 46 57 57 48 48 48
Columns 1213 through 1224
48 48 69 43 48 50 32 32 45 57 46 57
Columns 1225 through 1236
57 48 48 48 48 48 69 43 48 50 32 32
Columns 1237 through 1248
45 57 46 57 57 48 48 48 48 48 69 43
Columns 1249 through 1260
48 50 32 32 45 57 46 57 57 48 48 48
Columns 1261 through 1272
48 48 69 43 48 50 32 32 45 57 46 57
Columns 1273 through 1284
57 48 48 48 48 48 69 43 48 50 13 10
Columns 1285 through 1296
32 32 45 57 46 57 57 48 48 48 48 48
Columns 1297 through 1308
69 43 48 50 32 32 45 57 46 57 57 48
Columns 1309 through 1320
48 48 48 48 69 43 48 50 32 32 45 57
Columns 1321 through 1332
46 57 57 48 48 48 48 48 69 43 48 50
Columns 1333 through 1344
32 32 45 57 46 57 57 48 48 48 48 48
Columns 1345 through 1350
69 43 48 50 32 32
I'm viewing the file in notepad++
curoi
curoi 2013-9-9
编辑:curoi 2013-9-9
For Columns 889 through 900 in the decimal output, there are eight consecutive 32's which I'm guessing represent the spaces I don't want to be there (at least I don't want 6 of the 8 ). Again, this is all being viewed in notepad++.
Yup, those are spaces alright.
Notice that the preceding line is short, only 7 entries instead of 12, and notice that the short line has a 0.024 and a 0.040 that are out of place. Tracing back, we see that the most probably cause would be indx.lwr( p, 1 ) and indx.chl( r, 1 ) being unexpected values at some point, causing writing into rows before what are expected. Please go back and debug the relevant find() commands.
It is not clear why you would be using ":" as the first dimension when you are writing the output of the find() commands. Remember that an index of ":" in an assignment statement means "corresponding entries" if the right hand side is a vector, but if the right hand side comes out to be a scalar then the ":" as the index on the left means "all entries"
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 个)

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by