我设计了一个名为CLegend的应用程序,它允许构建一个多列图例,其代码为
function CLegend(hax,numcol,Ley)
%# Inputs
% hax : handle of the axes object to which belongs the legend
% numcol: number of columns for the legend
% Ley: text strings (labels) for the legend
set(hax,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
set(hax,'Units','characters');
posAx = get(hax,'Position');
insAx = get(hax,'TightInset');
[legend_h,object_h] = legend(hax,Ley,'Units','characters','Location',...
'South','Orientation','vertical');
posl = get(legend_h,'Position');
numlines = length(Ley);
if (numlines<numcol)
numcol = numlines;
end
numpercolumn = ceil(numlines/numcol);
if (mod(numlines,numpercolumn) == 0)
numcol = numlines/numpercolumn;
end
l = zeros(1,numlines);
a = zeros(1,numlines);
h = zeros(1,4);
for j=1:numlines
h = get(object_h(j),'Extent');
l(j) = h(3);
a(j) = h(4);
set(object_h(j),'Units','characters');
end
lmax = posl(3)*max(l);
hmax = posl(4)*max(a);
hLine = object_h(numlines+1);
xdata = get(hLine, 'xdata');
dx = xdata(2)-xdata(1);
di = 2;
sheight = hmax;
height = hmax*numpercolumn-sheight/2;
line_width = dx*posl(3);
spacer = xdata(1)*posl(3);
delta1 = spacer + line_width + spacer + lmax;
delta2 = line_width + spacer + lmax + spacer;
delta3 = lmax + spacer + line_width + spacer;
factx = 1/(posl(3)*numcol);
facty = 1/(hmax*numpercolumn);
width_l = numcol*delta1;
set(legend_h, 'Position', [posAx(1) + 0.5*(posAx(3)-width_l) posl(2) ...
width_l numpercolumn*hmax]);
col_ind = -1;
row_ind = -1;
j = 0;
for i=1:numlines,
if strcmpi(orient,'horizontal'),
if mod(i,numcol)==1,
row_ind = row_ind+1;
end
col_ind = mod(i,numcol)-1;
if col_ind == -1,
col_ind = numcol-1;
end
else
if numpercolumn==1 || mod(i,numpercolumn)==1,
col_ind = col_ind+1;
end
row_ind = mod(i,numpercolumn)-1;
if row_ind == -1,
row_ind = numpercolumn-1;
end
end
if (i==1)
linenum = i+numlines;
else
linenum = linenum+di;
end
labelnum = i;
set(object_h(linenum), 'ydata',facty*[height-row_ind*sheight ...
height-row_ind*sheight]);
set(object_h(linenum), 'xdata', factx*[spacer + j*delta2 ...
spacer + j*delta2 + line_width]);
set(object_h(linenum+1), 'ydata',facty*(height-row_ind*sheight));
set(object_h(linenum+1), 'xdata', factx*(spacer+line_width/2));
set(object_h(labelnum), 'Position', [j*delta3+spacer*2+line_width ...
height-row_ind*sheight]);
if (mod(i,numpercolumn)== 0)
j = j + 1;
end
end
opl = get(legend_h,'OuterPosition');
set(hax, 'Position',[posAx(1) posAx(2)+opl(4) posAx(3) posAx(4)-opl(4)]);
set(legend_h, 'OuterPosition',[opl(1) (posAx(2)-insAx(2))/2 opl(3) opl(4)]);
set([hax,legend_h],'Units','normalized');
end
我遇到了问题。我用一个更复杂的代码尝试了它,但类似于下面的这个代码。
function qq
fh = figure('Units','characters');
GrapWin = uipanel ('Parent',fh,'Units','characters','title','Graphic',...
'Position',[135 5 120 40]);
haxes = axes('Parent',GrapWin,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
PanVD = uipanel('Parent',fh,'Units','characters','Position',[55 5 30 10],...
'title','Dependent Variable');
VDlh = uicontrol('Parent',PanVD,'Style','listbox','Units',...
'normalized','Position',[0 0 1 1],'String',{'X','Y','Z'},'Value',1,...
'Callback',@VDLhCallback);
T = 0:pi/100:2*pi;
X = sin(T);
xlabel(haxes,'Time');
title(haxes,'Sine Function');
plot(haxes,T,X);
CLegend(haxes,3,{'Var X'});
function VDLhCallback (src,evt)
value = get(VDlh,'Value');
switch value
case 1
title(haxes,'Sine Function');
plot(haxes,T,X);
CLegend(haxes,3,{'Var X'});
case 2
title(haxes,'Cosine Function');
Y = cos(T);
plot(haxes,T,Y);
CLegend(haxes,3,{'Var Y'});
case 3
Z = tan(T);
title(haxes,'Tangent Function');
plot(haxes,T,Z);
CLegend(haxes,3,{'Var Z'});
end
end
end
它发生了一些我不理解的东西,因为第一次调用CLegend时,图例似乎没有居中(相对于轴),但是通过列表框选项进行的以下调用是居中的。 发生的其他问题是,如果我删除命令行 设定(haxes, '单元', '规范化') 然后,虽然图例居中,但轴的尺寸会减小。 我还发现Clegend第一次是calles,轴的位置(变量posAx)与下面的调用(posAx总是相同)不同。 我曾经想过,当单位被改变时,对象的位置没有改变,但是这个问题在我脑海里产生了怀疑。
答案 0 :(得分:1)
在Figure Properties documentation中,有人说Unit
此属性会影响CurrentPoint和Position属性。如果你 更改单位的值,最好将其返回到它 完成计算后的默认值,以免影响 假设单位为默认值的其他函数。
它可以解释你的问题吗?
答案 1 :(得分:0)
经过大量的证明,我发现我的问题在于调整数字的大小。 CLegend第一次被称为数字窗口的原始大小。以下CLegend调用发生在图形窗口最大化。