How to print using fprintf to distinguisish 1.1 from 1.10 and 1.100

12 Ansichten (letzte 30 Tage)
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?
The way I would like my txt file to be displayed is:
1.1,0
.
.
1.10,1,1.9
a=num2str(tribarray(k).pixel,precision);
fprintf(fid, '%s, %s, %s\n', a, num2str(size(tribarray(k).trib,2))); %,num2str(tribarray(k).trib));
%fprintf(fid, '%s, %s, %s\n', tribarray(k).pixel, size(tribarray(k).trib,2));
for j = 1:size(tribarray(k).trib,2)
b=num2str(tribarray(k).trib(j));
fprintf(fid, '%s,', b);
end
Thanks, Mariam

Akzeptierte Antwort

Guillaume
Guillaume am 1 Dez. 2014
Bearbeitet: Guillaume am 1 Dez. 2014
What do you mean distinguish? They're the exact same numbers:
a = 1.1;
b = 1.10;
isequal(a, b) %return true of course.
However many zeros you wrote at the end of the number initially is immediately lost as soon as you store the number into any numeric variable. Your options are thus:
1. store your numbers as string always, never as numeric even initially:
a = '1.1'
b = '1.10'
fprintf('%s, %s', a, b)
2. store your numbers as two integers, the integral part and the fractional part:
ai = 1; af = 1;
bi = 1; bf = 10;
fprintf('%d.%d, %d.%d', ai, af, bi, bf)
----
Side note: Don't do
fprintf('%s', num2str(anumber));
but
fprintf('%d', anumber) %for integers
fprintf('%f', anumber) %for real numbers
  2 Kommentare
Mariam
Mariam am 1 Dez. 2014
Thanks Guillaume,
Yes I understand they are the same number. It is just how I want to number my pixels. I have a grid of 102x108 pixels and the way I want to call each pixel is Row.Col so that is why I need the output file to distinguish pixel 1.1 from pixel 1.10 and similarly for 1.9 and 1.90 etc..
I used %.2f and removed the num2str but now I get all the numbers with tail zero 10.10 10.20 10.30 10.40
So my question is. Is there another way where the output would be 10.1 10.2 10.3 . . . 10.10?
So that it would display 2 digits after the decimal point only when its 10, 20 or 30?
Thanks, Mariam
Guillaume
Guillaume am 1 Dez. 2014
Ah, that's much simpler then:
fprintf('%d.%d', row, col);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Kelly Kearney
Kelly Kearney am 1 Dez. 2014
I would just do a simple concatenation, rather than try to figure out the proper %f precision for each:
[r,c] = meshgrid(1:108,1:102);
lbl = arrayfun(@(x,y) sprintf('%d.%d',x,y), r, c, 'uni', 0);

Mariam
Mariam am 1 Dez. 2014
Bearbeitet: Mariam am 1 Dez. 2014
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
  6 Kommentare
Stephen23
Stephen23 am 1 Dez. 2014
Yes, I just noticed Kelly's answer: they also give an excellent solution which keeps the row and column data separate.
Guillaume
Guillaume am 2 Dez. 2014
To this I would add, that using a dot to separate the two coordinates may not be the wisest idea, since here it is not used to separate the integral and fractional parts of a number as is the norm but to separate coordinates. Any other symbol would be better (except comma which is used for the same purpose in some languages)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by