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 comentarios

Stephen23
Stephen23 el 1 de Dic. de 2014
Editada: Stephen23 el 1 de Dic. de 2014
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
Stephen23 el 1 de Dic. de 2014
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 :)

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 1 de Dic. de 2014
Editada: Stephen23 el 1 de Dic. de 2014
Store your row and column data separately. There is no difference between 1.1 and 1.10 when it is stored as a floating point number, unless you store some kind of separate meta-data about each number... which would be more hassle than simply storing the row and column data separately.
Your easiest choice by far is to keep the row and column separate, and print then (if required) using Kelly's / Guillaume's solution to your earlier question:
fprintf('%d.%d', row, col)

Más respuestas (1)

per isakson
per isakson el 1 de Dic. de 2014
Try
>> fprintf( '%10.8f\n', 1 )
1.00000000
formatSpec — Format of the output fields string

4 comentarios

Mariam
Mariam el 1 de Dic. de 2014
This will treat both 1.1 and 1.10 similarly as well.
Both of them will be printed 1.1000000 and 1.1000000
I need the output to be 1.1 and 1.10 instead
Stephen23
Stephen23 el 1 de Dic. de 2014
They print the same because they are the same number.
Did you try to read the documentation?
>> fprintf( '%.1f and %.2f instead\n', 1.1, 1.1 )
1.1 and 1.10 instead
Stephen23
Stephen23 el 1 de Dic. de 2014
Editada: Stephen23 el 1 de Dic. de 2014
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Dic. de 2014

Comentada:

el 1 de Dic. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by