# 2 digits after the dot

조회 수: 4 (최근 30일)
Shahar ben ezra 2021년 4월 11일
편집: dpb 2021년 4월 12일
Hi
In the following example where do I put: ,'%0.2f'
x = [1 2 3];
vals = [2 3.3333333 6; 11 23 26];
b = bar(x,vals);
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center','VerticalAlignment','bottom')
xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;
labels2 = string(b(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center','VerticalAlignment','bottom')
Same problem here:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
my_num= 3.33333
text_information =['my num is : ' num2str((my_num))];
xlabel(text_information); xlabel(text_information,'interpreter','latex')
tnx :)

댓글을 달려면 로그인하십시오.

### 채택된 답변

dpb 2021년 4월 11일
편집: dpb 2021년 4월 12일
In the first case, you'll have to use
>> string(arrayfun(@(n)sprintf('%.2f',n),vals,'UniformOutput',false))
ans =
2×3 string array
"2.00" "3.33" "6.00"
"11.00" "23.00" "26.00"
>>
because the string class 'fmt' optional input is only recognized for datetime or duration arguments, not numbers.
num2str() accepts a trailing format string, but has some warts that have to be aware of -- to illustrate:
>> num2str(vals(1,:),'%.2f')
ans =
'2.003.336.00'
>> num2str(vals(1,:).','%.2f')
ans =
3×4 char array
'2.00'
'3.33'
'6.00'
>>
in the first case, if just take the wanted array row, the output of the row vector isn't three strings but only one because num2str isn't smart enough to be able to do other than just catenate the three strings in a row then return the smooshed-together char() string.
If you transpose and pass a column vector instead, then it uses strvcat() internally and you do get a char() array--but again, that's a char() array and so to use it you have to remember that each element in the array is just a single byte. It will also pad the shorter strings to the maximum length of any to retain rectangular array -- in this case that doesn't show up, in some cases the extra white space may be annoying if the usage doesn't automagically strtrim() the result.
In the second case, since you just have the single value, you can add the format string to str2num
xlabel(['my num is : ' num2str(my_num,'%0.2f')],'interpreter','latex')
NB: you don't need either the temporary variable to hold the string (although that may be convenient in more complicated cases) nor to call xlabel twice to pass the string and also set the interpreter property.
You can also avoid the catenation of the string by putting the literal text in the format string--
xlabel(num2str(my_num,'my num is: %0.2f'),'interpreter','latex')
See the doc for fprintf for all the skinny on formatting strings rules.
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Stephen23 2021년 4월 12일
"In the first case, you'll have to use ... because the string class 'fmt' optional input is only recognized for datetime or duration arguments, not numbers."
It is simpler to use compose:
vals = [2,3.3333333,6;11,23,26];
str = compose("%0.2f",vals)
str = 2×3 string array
"2.00" "3.33" "6.00" "11.00" "23.00" "26.00"
Shahar ben ezra 2021년 4월 12일
now I get it
Thank you!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by