Effacer les filtres
Effacer les filtres

Why doesn't my code to insert and format a legend work in 2014b?

1 vue (au cours des 30 derniers jours)
Brett Mather
Brett Mather le 17 Fév 2015
Modifié(e) : Brett Mather le 17 Fév 2015
I have read that the legend no longer has children in 2014b, and that I should use the output arguments from the legend function to get the handles for these objects (<http://www.mathworks.com/help/matlab/graphics_transition/why-is-the-children-property-empty-for-some-objects.html)>.
However, the legend function doesn't seem to work properly with these output agruments. I have put together some code to show my problems with the legend output arguments and formatting.
clear all
close all
clc
%%%%%creating 1st figure
f=figure;
ax=axes;
p1=plot(1:10,1:10);
hold all
p2=plot(10:-1:1,1:10);
%%this line returns a warning:
[mylegend1,legend_items1]= legend(ax,{'a','b'}, 'FontSize', 4,'Orientation','horizontal','Units','pixels','Interpreter', 'none');
% Warning: Error updating Legend. Following is the chain of causes of the error:
%
% Attempted to access lpos(3); index out of bounds because numel(lpos)=2.
%
% > In defaulterrorcallback at 12
% In C:\Program Files\MATLAB\R2014b\toolbox\matlab\scribe\private\legendHGUsingMATLABClasses.p>find_legend_info at 382
% In C:\Program Files\MATLAB\R2014b\toolbox\matlab\scribe\private\legendHGUsingMATLABClasses.p>legendHGUsingMATLABClasses at 251
% In legend at 118
%%%%%creating 2nd figure
f2=figure;
ax2=axes;
p3=plot(1:10,1:10);
hold all
p4=plot(10:-1:1,1:10);
%%by removing the legend_items output argument there is no warning:
mylegend2= legend(ax2,{'a','b'}, 'FontSize', 4,'Orientation','horizontal','Units','pixels','Interpreter', 'none');
%%%%%creating 3rd figure
f3=figure;
ax3=axes;
p5=plot(1:10,1:10);
hold all
p6=plot(10:-1:1,1:10);
%multiple output arguments returns no warnings without the property changes
%that I need
[mylegend3,legend_items3]= legend(ax3,{'a','b'});
%attempting to set the properties after the fact doesn't work as expected;
%especially seemsto have trouble with Orientation 'horizontal'
set(mylegend3,'FontSize', 4,'Orientation','horizontal','Units','pixels','Interpreter', 'none')

Réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by