多行注释:num2str 的问题

Multiline annotation: problem with num2str

我希望文本框中的文本显示为:

Simulation parameters: 
Number of loops = 500
Number of subcarriers = 12
Number of frames = 5,
MT = 2, MR = 2               % or MR x MT = 2 x 2

我的代码写成:

txt = {'Simulation parameters: ','Number of loops = ',  num2str(loops_num),'Number of subcarriers = ',num2str(Nfft),,'Number of frames = ',num2str(K),'MT = ',num2str(MT),'MR = ',num2str(MR)};
text(4,0.5,txt,'FontSize',12)

我得到了:

1) 如何解决? (Number of subcarriers = 100, etc需要在一行)

2) 这种情况下如何使用annotation

plot(1:10)
str = {'Simulation parameters: ','Number of loops = ',  num2str(loops_num),'Number of subcarriers = ',num2str(Nfft),'MT = ',num2str(MT),'MR = ',num2str(MR),'Number of frames = ',num2str(K)};
annotation( 'textbox', 'String',str, 'Color', 'black', ...
            'FontSize', 14, 'Units', 'normalized', 'EdgeColor', 'none', ...
            'Position', [0.8,0.5,0.2,0] )
set( gca, 'Position', [0.1, 0.1, 0.6, 0.8] )

按原样创建一个字符数组元胞数组 ("strings"),在 annotation 中使用时将其解释为单独的行。因此,您必须连接那些您希望必须使用 [ ... ] 合并到一行的值。请参阅以下简短示例:

plot(1:10);
str = { ...
  'Simulation parameters: ', ...
  ['Number of loops = ', num2str(500)], ...
  ['Number of subcarriers = ', num2str(12)], ...
  ['MT = ', num2str(2)], ...
  ['MR = ', num2str(2)], ...
  ['Number of frames = ', num2str(5)] ...
};
annotation('textbox', [0.3, 0.6, 0, 0], 'String', str, 'FitBoxToText', 'on');

输出(Octave 5.1.0;代码也在 MATLAB Online 中测试)如下所示:

希望对您有所帮助!


编辑: 关于这个问题,注释是否可以移到图中之外。有可能,BUT 包含操纵图形属性。这是修改后的代码:

plot(1:10);
set(gca, 'Position', [0.1 0.1 0.6 0.8]);
str = { ...
  'Simulation parameters: ', ...
  ['Number of loops = ', num2str(500)], ...
  ['Number of subcarriers = ', num2str(12)], ...
  ['MT = ', num2str(2)], ...
  ['MR = ', num2str(2)], ...
  ['Number of frames = ', num2str(5)] ...
};
annotation('textbox', [0.75, 0.60, 0, 0], 'String', str, 'FitBoxToText', 'on');

这是更新后的输出:

Matlab不知道你要把文本和数字放在一起,因为它们都是单独的字符串,你需要把文本和数字组合在一起,这样Matlab才知道把它们放在一起,例如:

txt = {'Simulation parameters: ',['Number of loops = ',  num2str(loops_num)],['Number of subcarriers = ',num2str(Nfft)],['Number of frames = ',num2str(K)],['MT = ',num2str(MT)],['MR = ',num2str(MR)]};
text(4,0.5,txt,'FontSize',12)

str = {'Simulation parameters: ',['Number of loops = ',  num2str(loops_num)],...
                                 ['Number of subcarriers = ',num2str(Nfft)],...
                                 ['MT = ',num2str(MT)],...
                                 ['MR = ',num2str(MR)],...
                                 ['Number of frames = ',num2str(K)]};
annotation( 'textbox', 'String',str, 'Color', 'black', ...
            'FontSize', 14, 'Units', 'normalized', 'EdgeColor', 'none', ...
            'Position', [0.8,0.5,0.2,0] )

我使用

组合字符串和变量
[ 'text = ' num2str(value)];

您也可以使用

sprintf ( 'text = %f', value )

它有点难读,但它更强大且速度更快(对于一次性转换,这是无关紧要的,但了解以备将来参考很有用)

sprintf documentation for more info