formatting understanding with different specifiers
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Walter Roberson
2016년 2월 29일
The "format" command has no interaction with fprintf() or sprintf() formatting at all.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!