How can I force MatLab to display integers in function sprintf ?

Hi,
I would like to save graphs in a loop and use sprintf to change the file names, but don't want to have decimal places because this destroys the file extension. The loop is:
for i=1:5
x = [0 ; 0.44; 1.1; -1,1; -0.44];
sprintf('Graph %2d.png',x(i)*100)
end
When I use x(3) =1.1 sprintf('Graph %2d.png',x*100) gets displayed as " Graph 1.100000e+02 ". The same problem does not occur with sprintf('Graph %2d.png',110). If x = 0.44 is also works fine. How can I force MatLab to display integers for every number?
Thanks for your help!!

 採用された回答

the cyclist
the cyclist 2014 年 6 月 10 日

0 投票

Due to the limits of floating point arithmetic, 1.1*100 is not precisely represented as 110.
You could try
round(x(i)*100)

3 件のコメント

Julia
Julia 2014 年 6 月 10 日
Thank you!
Sean de Wolski
Sean de Wolski 2014 年 6 月 10 日
I recommend using the '%04i' syntax which will maintain alphabetical order:
sprintf('%03i',6)
Stephen23
Stephen23 2015 年 3 月 3 日
Actually sprintf already preforms a round operation internally, so adding an explicit round is unnecessary and just slows it down. If you want to guarantee that the output does not use exponent notation, then %.0f format is best:
>> sprintf('%03.0f',456.9)
ans = 457
Note the rounding!

サインインしてコメントする。

その他の回答 (1 件)

Devaraja Lakshmanappa
Devaraja Lakshmanappa 2015 年 3 月 3 日

0 投票

I had used mprintf function in my program,but i am getting error saying this function is not there in matlab7.0.2.

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

タグ

質問済み:

2014 年 6 月 10 日

コメント済み:

2015 年 3 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by