Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab-绘图标题中数字变量的简短格式_Matlab_Plot_Matlab Figure_Number Formatting - Fatal编程技术网

Matlab-绘图标题中数字变量的简短格式

Matlab-绘图标题中数字变量的简短格式,matlab,plot,matlab-figure,number-formatting,Matlab,Plot,Matlab Figure,Number Formatting,我试图将变量放入绘图标题中,但无法生成小数点后4位的格式。如何避免标题中的浮动格式 这是我使用的代码 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}) 您需要使用%f作为。更改代码 Mean=sprintf('Mean=%0.4d',M) 到 将以小数点后4位的精度打印M。如果要打印不带任何小数点的M,则需要使

我试图将变量放入绘图标题中,但无法生成小数点后4位的格式。如何避免标题中的浮动格式

这是我使用的代码

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})

您需要使用
%f
作为。更改代码

Mean=sprintf('Mean=%0.4d',M)

将以小数点后4位的精度打印
M
。如果要打印不带任何小数点的
M
,则需要使用
%.0f

Mean=sprintf('Mean=%.0f',M)
%.0f
将打印一个带0位小数的
双精度
浮点
值,并显示为打印一个带
%d
的整数


如果变量
X
包含
N
元素,则使用内置的MATLAB函数将产生与
sum(X)/N
相同的输出



注意:应注意变量名。MATLAB包含一个
mean()
函数,您不希望通过调用变量
mean
来覆盖该函数。MATLAB变量名区分大小写,因此
Mean
可以,但
Mean
不行。

您可以使用
num2str
函数。例如:

mean(y)
a=num2str(ans,5)
plot(x,y)
title(['sometext ' a])
5
中的
num2str
参数显示最大有效位数。而
a
现在是
字符串
。 顺便说一下,您可以使用
mean
内置函数代替公式

mean(y)
a=num2str(ans,5)
plot(x,y)
title(['sometext ' a])