【问题标题】:Add multiple text blocks to a figure in Matlab在 Matlab 中将多个文本块添加到图形中
【发布时间】:2012-12-01 10:05:12
【问题描述】:

我正在尝试找到一种更好\更紧凑的方法来将许多“文本块”(我称“文本块”一些具有多行的文本)添加到图形中。例如下图:

我用来生成这个图的代码是:

x=-4*pi:pi/20:4*pi;
y=cos(x);

% generate positions for the text blocks
[ypos,xpos]=find(ismember(y,max(y))); % finds multiple maxima

% generate random data to be presented
info1=rand(1,numel(xpos));     info2=rand(1,numel(xpos));     info3=rand(1,numel(xpos));

plot(x,y);
ylim([-1.1 1.4])

% generate the "text blocks"
text(x(xpos),ypos+0.3,strcat('A_{',num2str((1:numel(xpos))'),'}=',num2str(info1','%0.1f')),'FontSize',8);    
text(x(xpos),ypos+0.2,strcat('B_{',num2str((1:numel(xpos))'),'}=',num2str(info2','%0.1f')),'FontSize',8);    
text(x(xpos),ypos+0.1,strcat('C_{',num2str((1:numel(xpos))'),'}=',num2str(info3','%0.1f')),'FontSize',8);    
%...
%... etc

我知道可以使用元胞数组添加多行文本字符串,方法是将字符串变量定义为每个元胞一行一行的元胞数组。例如:

 text(x,y,{'line 1' ; 'line 2' ; 'line 3'});

但是,这也需要每个文本块一行代码,所以它不能解决多文本块的情况...

我的问题是: 有没有办法在单行或更一般的意义上添加这些文本块,比如我有 n 个文本块,每个文本块都有一些可变数量的文本行?

【问题讨论】:

  • 点是你想要一种单班轮版本吗?

标签: matlab plot matlab-figure


【解决方案1】:

怎么样

pos = num2cell(linspace(.3,.1,3));
lett = num2cell(linspace('A','C',3));

your_text_f_handle = @(ps,lt) ...
    text(x(xpos),ypos+ps,strcat(lt, ... 
    '_{',num2str((1:numel(xpos))'),'}=',... % //REM: old info_i are here
     num2str(rand(1,numel(xpos))','%0.1f')),'FontSize',8);

cellfun(your_text_f_handle ,pos,lett);

它提供完全相同的输出(经过测试),并且易于扩展。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2017-10-23
    • 2013-03-05
    • 1970-01-01
    • 2015-03-02
    相关资源
    最近更新 更多