Matlab-绘图标题中数字变量的缩写格式 [英] Matlab - short format of number variable in the plot title

查看:188
本文介绍了Matlab-绘图标题中数字变量的缩写格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将变量放入绘图标题中,但是无法产生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})

推荐答案

您需要使用%f作为

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位的doublefloat值,并且看起来就像您用%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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆