formatting understanding with different specifiers

2 ビュー (過去 30 日間)
Robert
Robert 2016 年 2 月 28 日
コメント済み: Stephen23 2016 年 2 月 29 日
Im confused over the following field specifiers
I was always under the impression and also by definition in matlab documentation of the following
format short was 5 characters long it shows as 4 because the 5 is a round digit and is "hiden" I have used this to my advantage actually several times
Same with format long
only 16 characters and the 16th is hidden showing 15
for example
EDU>> format short
EDU>> x = 1/3
x =
0.3333
EDU>> format long
EDU>> x = 1/3
x =
0.333333333333333
If you take your time counting them you will see there is 5 and 15 as expected
However I'm confused about the specifics of the conversion specifier d and f
format short
for k =1:9
fprintf('%f\n',k);
end
If you run this foolish little bit of code I'm getting the following result
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
obviously this is showing 7 digits Why am i getting this result?
From what i have always seen i should get
1.0000 for a total of 6 with one hidden etc
Also if i run the same loop with d
format short
for k =1:9
fprintf('%d\n',k);
end
of course i get 1 2 3 4 ......
Now according to the matlab help file %d is
Decimal notation (signed)
So can someone explain to me why i am getting what I'm getting? Thanks in advance
  1 件のコメント
Stephen23
Stephen23 2016 年 2 月 29 日
format and the output of fprintf are completely unrelated to each other.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 2 月 29 日
The "format" command has no interaction with fprintf() or sprintf() formatting at all.

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by