Formatting output text file problem
古いコメントを表示
I am working on producing tributary pixels input file to GAMS and I need them to be numbered in a way that distinguishes 1.1 from 1.10 and 1.100. Is there a way to print a string of characters distinguishing these differences?
To make my question clearer I have a the structure shown below. The first column is pixel number and the second has pixels flowing into this pixel. I want to print this structure into a file such that 1.1 and 1.10 are two different pixels.
1.10000000000000 0
1.20000000000000 0
1.30000000000000 0
1.40000000000000 0
1.50000000000000 0
1.60000000000000 0 1.70000000000000 0 1.80000000000000 0 1.90000000000000 0 1.10000000000000 0
2.10000000000000 [1.10000000000000,0]
2.20000000000000 [1.20000000000000,0]
2.30000000000000 [1.30000000000000,0]
2.40000000000000 [1.40000000000000,0]
2.50000000000000 [1.50000000000000,0]
2.60000000000000 [1.60000000000000,0]
2.70000000000000 [1.70000000000000,0]
2.80000000000000 [1.80000000000000,0]
2.90000000000000 [1.90000000000000,0]
2.10000000000000 [1.10000000000000,0]
3.10000000000000 [2.10000000000000,0]
. . .
10.2000000000000 [10.1000000000000,9.20000000000000,0]
10.3000000000000 [10.2000000000000,9.30000000000000,0]
10.4000000000000 [10.3000000000000,9.40000000000000,0]
10.5000000000000 [10.4000000000000,9.50000000000000,0]
10.6000000000000 [10.5000000000000,9.60000000000000,0]
10.7000000000000 [10.6000000000000,9.70000000000000,0]
10.8000000000000 [10.7000000000000,9.80000000000000,0]
10.9000000000000 [10.8000000000000,9.90000000000000,0]
10.1000000000000 [10.9000000000000,9.10000000000000,0]
I used this code:
fid = fopen('UBNTrib_mariam1.txt', 'w');
fprintf('Neighboring Tribs List\n');
fprintf('pixel,Num tribs, Tributaries');
for k=1:size(tribarray,2)
num2str(tribarray(k).pixel),num2str(tribarray(k).trib));
fprintf(fid, '%s, %s, %s\n', num2str(tribarray(k).pixel), num2str(size(tribarray(k).trib,2)));
for j = 1:size(tribarray(k).trib,2)
fprintf(fid, '%s,', num2str(tribarray(k).trib(j)));
end
fprintf(fid, '\n');
end
fclose(fid);
% view the contents of the file type UBNTrib_mariam1.txt;
'done'
However the output is: Neighboring Tribs List pixel,Num tribs, Tributaries
1.1, 1, 0,
1.2, 1, 0,
1.3, 1, 0,
. . .
10.1, 3, 10.9,9.1,0,
so the last line for instance should be : 10.10 , 3 , 10.9, 9.10,0
Thanks a lot, Mariam
2 件のコメント
You say that "I have a the structure shown below.", but it is not clearly explained how this data is stored: is this in a file, or some kind of array within MATLAB? (A structure is a specific data class in MATLAB).
It seems that the aim is to distinguish between your input data ("structure") values 1.10000000000000 and 1.10000000000000, where one should be saved as 1.1 and the other as 1.10 . But how exactly do we know which is which? They are in fact the same number.
This issue was already addressed very comprehensively by Guillaume in your earlier question: http://www.mathworks.com/matlabcentral/answers/164978
Stephen23
2014 年 12 月 1 日
You tell us that "the last line for instance should be : 10.10 , 3 , 10.9, 9.10,0", but omit to give the raw data for this line. Supplying this might help clarify the situation a little more :)
採用された回答
その他の回答 (1 件)
per isakson
2014 年 12 月 1 日
Try
>> fprintf( '%10.8f\n', 1 )
1.00000000
formatSpec — Format of the output fields string
4 件のコメント
Mariam
2014 年 12 月 1 日
Stephen23
2014 年 12 月 1 日
They print the same because they are the same number.
per isakson
2014 年 12 月 1 日
Did you try to read the documentation?
>> fprintf( '%.1f and %.2f instead\n', 1.1, 1.1 )
1.1 and 1.10 instead
Using the format spec will allow the number of decimal places to be specified, but it is not yet clear to me how this information is encoded in the original data: exactly how are 1.10000000000000 and 1.10000000000000 different from one another?
And even then, trying to create fprintf format strings on-the-fly is something that is best avoided... this is a great path to obfuscated code.
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!