简体   繁体   English

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

[英]Matlab: apply scientific notation in textbox

I want to add a textbox to a plot in which a number is displayed in scientific notation.我想将一个文本框添加到一个以科学记数法显示数字的图中。 Here my code:这是我的代码:

fano = 74585849.3443; figure; plot(x,y) annotation('String',['fano =',num2str(fano)],'FitBoxToText','on'); Here fano is displayed as decimal number.这里 fano 显示为十进制数。 How can I change the format to scientific notation?如何将格式更改为科学记数法?

You can just create the desired string with the function sprintf .您可以使用函数sprintf创建所需的字符串。 Here are three examples with a different number of decimal places printed.以下是三个打印不同小数位数的示例。 When formatting the text with %M.Nd, M specifies the field width, N the precision (number of decimal places) and d means decimal (signed).使用 %M.Nd 格式化文本时,M 指定字段宽度,N 指定精度(小数位数),d 表示小数(带符号)。 Below are three different examples with 2, 5 and 8 decimal places.以下是三个不同的示例,分别带有 2、5 和 8 位小数。 dim is an array with the location and size of the textbox in normalized units with respect to the figure size, formatted as [x_location y_location width height]. dim 是一个数组,文本框的位置和大小以相对于图形大小的标准化单位表示,格式为 [x_location y_location width height]。 In your case width and height don't matter, since you are using the 'FitBoxToText' property.在您的情况下,宽度和高度无关紧要,因为您使用的是 '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');

Here is the output:这是输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM