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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by