您可以使用函数 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');
这是输出: