Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab中图例中左右文本对齐的组合_Matlab_Text_Plot_Graph_Legend - Fatal编程技术网

Matlab中图例中左右文本对齐的组合

Matlab中图例中左右文本对齐的组合,matlab,text,plot,graph,legend,Matlab,Text,Plot,Graph,Legend,我想让我的传奇在Matlab中看起来像这样 [类别1 1.5%] [类别2.3%] 其中文本向左调整,百分比向右调整。 有没有办法做到这一点?我想在图例中的每个条目中添加第二个文本对象,但我想不出一种方法。现有的图例文本对象将图例本身作为父对象,但是,我无法向其添加新的文本对象。即使尝试按值复制文本对象也不起作用: [l_h, object_h] = legend({'A', 'B'}); text_h = findobj(object_h,'Type','Text'); copyobj(te

我想让我的传奇在Matlab中看起来像这样

[类别1 1.5%]

[类别2.3%]

其中文本向左调整,百分比向右调整。 有没有办法做到这一点?我想在图例中的每个条目中添加第二个文本对象,但我想不出一种方法。现有的图例文本对象将图例本身作为父对象,但是,我无法向其添加新的文本对象。即使尝试按值复制文本对象也不起作用:

[l_h, object_h] = legend({'A', 'B'});
text_h = findobj(object_h,'Type','Text');

copyobj(text_h(1), text_h(1).Parent)
给我

使用copyobj时出错

文本不能是图例的子级


您可以使用
sprintf
的技巧,并使用每个字符具有恒定间距的字体,例如courier:

% demo from legend help:
x = 0:.2:12;
plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x));
% 3 legend entries which will have "TXT NUMBER%" format
text = {'First 10%','Second 20%','Third 30%'};
% make a copy of the text
numbers = text;

% split the text and numbers into two cell arrays
%    You probably have them split before here in your
%    code - I just do it here for demo.
for ii=1:length(text)
  entries = strsplit ( text{ii} );
  text{ii} = entries{1};
  numbers{ii} = entries{2};
end

% find the longest text entry.
mlen = max(cellfun ( @length, text ));

% create your format string - left justify the text.
formatstr = sprintf ( '%%-%is %%s', mlen );

% update the text which will go in the legend
for ii=1:length(text)
  text{ii} = sprintf ( formatstr, text{ii}, numbers{ii} );
end

% create the legend 
h = legend ( text );
% Change the font name - this is important!!
h.FontName = 'Courier';

好的,通过玩游戏,我找到了一种方法。 您不能在图例对象下创建新的文本对象,但是,还有另一个变换对象,其中包含可用于相同目的的图例标记。不确定逻辑是什么,但这是可行的:

[l_h, object_h] = legend({'A', 'B'});
text_h = findobj(object_h,'Type','Text');
marker_h=findobj(object_h,'Type','Patch');
legend_parent_h = marker_h(1).Parent;

p_h = copyobj(text_h(i), legend_parent_h);    

% now anything can be done with p_h

谢谢你的建议:不幸的是,这不适合我,因为我需要使用一种不固定宽度的特定字体。