How to print using fprintf to distinguisish 1.1 from 1.10 and 1.100
古いコメントを表示
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
採用された回答
その他の回答 (2 件)
Kelly Kearney
2014 年 12 月 1 日
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);
6 件のコメント
Stephen23
2014 年 12 月 1 日
Mariam: you already accepted an answer for this question, which means that other people browsing MATLAB Answers will:
- think that your question has been been resolved,
- not be able to gain reputation by actually answering the question.
Mariam
2014 年 12 月 1 日
MATLAB, a new forum... it all takes time to learn :)
For your question, Guillaume gave an excellent explanation and answer:
fprintf('%d.%d', row, col)
Keep the two parts separate, and then it will be much easier to deal with.
Star Strider
2014 年 12 月 1 日
That was also the substance of Kelly’s earlier Answer.
Stephen23
2014 年 12 月 1 日
Yes, I just noticed Kelly's answer: they also give an excellent solution which keeps the row and column data separate.
Guillaume
2014 年 12 月 2 日
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)
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!