MATLAB: How to prevent the legend from REALLY auto-updating

autoupdatelegend

I'm plotting graphs of the same type in multiple subplots, with some subplots left blank. I'd like to use the blank spots to plot a legend. I could of course move the location of the legend but that's really tedious. The seemingly obvious, clean way to do this is as in the following code. In the sixth plot region, I plot my lines, create the legend, set autoupdate to off, then set the linestyles to 'none'. Trouble is: when I set the linestyles to none, the legend autoupdates and sets the legend lines to none as well. So what I need to do is break the link between the actual lines and the legend lines.
close all;
for ii=1:2:9;
subplot(3,3,ii);
clear h;
hold on;
h(1) = plot(1:10);
h(2) = plot(10:-1:1);
if ii==5;
subplot(3,3,6);
hold on;
h(1) = plot(1:10);
h(2) = plot(10:-1:1);
legend('up','down','AutoUpdate','off');
for ii=1:numel(h);
h(ii).LineStyle='none';
end;
end;
end;
A hideous kludge is the following: close all;
for ii=1:2:9;
subplot(3,3,ii);
clear h;
hold on;
h(1) = plot(1:10);
h(2) = plot(10:-1:1);
if ii==5;
subplot(3,3,6);
hold on;
h(1) = plot(11:20);
h(2) = plot(20:30);
legend('up','down','AutoUpdate','off');
set(gca,'YLim',[1,10]);
end;
end
But this is unsatisfactory in so many ways that I won't bother to enumerate them. So the question is, is there a clean way to do what I want to do, just by breaking the link between actual plot lines and legend lines, and not resorting to a hideous kludge?

Best Answer

Actually, it's simpler than outlined; that kept extra lines around; don't even need that; just use the builtin feature of plot() to not show NaN values...
for ii=1:2:nSubplots
...
subplot(3,3,ii);
...
plots here
end
hAx=subplot(3,3,6); % for legend
hL(1)=plot(nan(1,1)); % create the line style; doesn't show
hL(2)=plot(nan(1,1));
hLg=legend('up','down','AutoUpdate','off');
delete(hL); % can even remove the objects
hAx.Color='none'; % just leave the legend visible
hAx.XColor='none';
hAx.YColor='none';
Salt to suit re: box outline, etc., etc., etc., ...
ADDENDUM Better way to hide the unwanted axis than zzz.Color='none' is
hAx.Visible='off';