Matlab:在文本框中应用科学记数法

时间:2017-09-28 06:45:29

标签: matlab textbox scientific-notation

我想在绘图中添加一个文本框,其中数字以科学计数法显示。 这是我的代码:

fano = 74585849.3443; figure; plot(x,y) annotation('String',['fano =',num2str(fano)],'FitBoxToText','on'); 这里fano显示为十进制数。如何将格式更改为科学记数法?

1 个答案:

答案 0 :(得分:2)

您可以使用 sprintf 功能创建所需的字符串。以下是三个打印小数位数不同的示例。使用%M.Nd格式化文本时,M指定字段宽度,N指定精度(小数位数),d表示十进制(带符号)。以下是三个不同的例子,小数点后2位,5位和8位。 dim是一个数组,其中文本框的位置和大小以标准化单位表示,相对于图形大小,格式为[x_location y_location width height]。在你的情况下,宽度和高度并不重要,因为你正在使用' FitBoxToText'属性。

fano = 74585849.3443;
figure;
x = 0:0.01:10;
y = sin(2*pi*1*x);
plot(x,y);
dim = [.5 .85 .0 .0];
str = sprintf('fano = %0.2d',fano);
annotation('textbox',dim,...
    'String',str,...
    'FitBoxToText','on',...
    'BackgroundColor','white');
dim = [.5 .65 .0 .0];
str = sprintf('fano = %0.5d',fano);
annotation('textbox',dim,...
    'String',str,...
    'FitBoxToText','on',...
    'BackgroundColor','white');
dim = [.5 .45 .0 .0];
str = sprintf('fano = %0.8d',fano);
annotation('textbox',dim,...
    'String',str,...
    'FitBoxToText','on',...
    'BackgroundColor','white');

这是输出:

enter image description here