Matlab-绘图标题中数字变量的缩写格式 [英] Matlab - short format of number variable in the plot title
问题描述
我正在尝试将变量放入绘图标题中,但是无法产生4个小数位的格式.如何避免标题中出现浮点格式?
I am trying to place a variable into a plot title but I cannot produce formatting of 4 decimal places. How to avoid the float format in the title?
这是我使用的代码
subplot(3,2,1);
hist(X,10);
str=sprintf('X N=%d,Y=%d',N,Y)
M=sum(X)/N
Mean=sprintf('Mean=%0.4d',M)
title({str,Mean})
推荐答案
You need to use %f
as the format specifier for float values. Changing your code
Mean=sprintf('Mean=%0.4d',M)
到
Mean=sprintf('Mean=%0.4f',M)
将打印精度为小数点后4位的M
.如果要打印不带小数位的M
,则需要使用%.0f
will print M
with 4 decimal places of accuracy. If you want to print M
without any decimal places then you need to use %.0f
Mean=sprintf('Mean=%.0f',M)
%.0f
将打印一个带有小数点后0位的double
或float
值,并且看起来就像您用%d
打印一个整数一样.
%.0f
will print a double
or float
value with 0 decimal places and appear as if you printed an integer with %d
.
如果变量X
具有N
个元素,则比使用内置的MATLAB函数 mean()
将产生与sum(X) / N
相同的输出.
If your variable X
has N
elements than using the builtin MATLAB function mean()
will produce the same output as sum(X) / N
.
注意:您应注意变量名. MATLAB包含一个mean()
函数,您不想通过调用变量mean
来覆盖它. MATLAB变量名称区分大小写,因此Mean
可以,但mean
不能.
Note: You should be careful of your variable names. MATLAB includes a mean()
function which you don't want to overwrite by calling a variable mean
. MATLAB variable names are case-sensitive so Mean
is ok but mean
isn't.
这篇关于Matlab-绘图标题中数字变量的缩写格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!