フィルターのクリア

reducing decimals and using as string

81 ビュー (過去 30 日間)
Reza S
Reza S 2015 年 3 月 6 日
コメント済み: Reza S 2015 年 3 月 6 日
Hello,
I am going to truncate or round the decimals of a variable to the maximum of 2 decimal and then use num2str to shown the resulted number on a plot title. I am getting the error message when I use round()? Also I am not sure how to use fprint in this case. Any body can help please or any better solution?
Thanks
  1 件のコメント
Jan
Jan 2015 年 3 月 6 日
Please post the error message and the code. Obviously there is a bug.

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

採用された回答

Stephen23
Stephen23 2015 年 3 月 6 日
編集済み: Stephen23 2015 年 3 月 6 日
You do not need to use round, as num2str already has a precision option:
>> num2str(pi,2)
ans = '3.1'
You could also use sprintf which converts a number to string and gives control over many format options, including the number of significant digits:
>> sprintf('%.2g',pi)
ans = '3.1'
If you are trying to use this in a plot title, than using fprintf will not help you: fprintf prints text to a file or the command window. If you want the title of a plot then you need a string and the title function, like this:
>> str = num2str(pi,2)
>> title(str)
  1 件のコメント
Reza S
Reza S 2015 年 3 月 6 日
Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by