Locking the Legend even if the object is deleted

Hello,
I want the AutoUpdate of my legend to be "off" no matter whether I delete an object from the axes, but:
(since R2022b): If you delete an object from the axes, the legend updates to reflect the change regardless of whether this property (AutoUpdate) is set to "on" or "off".
I have programmed a simulation where the legend of the axes should stay as it is, no matter which objects you delete in the plot. Do you have any idea how I can implement this in newer versions of Matlab?
Warm regards,
Ruben

 Accepted Answer

As @Bjorn Gustavsson suggests, your best bet is to create proxy objects that are represented in the legend, but are not visible in the axes. @Bjorn Gustavsson says to place them "outside the visible area", but my recommendation is to use NaN data so they are not visible. For example:
% Objects with real data.
p = plot(magic(5));
% Create proxy objects with NaN data, which will now be rendered, but can
% be put into the legend.
hold on
pLegend = plot(NaN(5)); % Proxy objects with NaN data.
hold off
% Copy the SeriesIndex from the objects with real data to the proxy
% objects, which will make the colors and line styles match.
set(pLegend, {'SeriesIndex'},{p.SeriesIndex}');
% Use the proxy objects in the legend.
legend(pLegend);
% Now you can delete the real objects, but the proxy object will remain in the legend.
delete(p(3))

5 Comments

Nifty trick to use nan to create objects that are invisible but still have all relevant properties! I never bothered thinking of testing that after I realized I could hide stuff outside the window...
Thank you very much for your answers. The problem is, that my model is a bit more complex and at some point I basically have to "cla" the graph but still want to keep the legend (I'm displaying a map which includes cars, charging stations, routes, etc). Before R2022b this was not a problem, because "cla" still kept the legend as it was (I did use nan-object to indicade cars, charging stations, routes, etc and of course had "AutoUpdate" turned "off") but as of R2022b the behaviour changed and I was left with empty legends.
Right now my workaround is to create the "proxy object" everytime when I am "clearing the map" and re-create the legend. Basically the R2022b Update's feature of "AutoUpdate="off" does now not work on cla-commands anymore" turns out to be a bug for me in my simulation and now I'm wondering if there is a better workaround than mine. I hope it makes sense.
If you do a cla on an axis surely you have to redraw any background map and other basic stuff. Would it be possible for you to keep an array (cell-array) with handles to all objects you draw ontop of the map and want to periodically remove - then you can simply delete them as you see fit without having to start over from scratch. That's what I'd try if I started today with a project with your description.
I agree with @Bjorn Gustavsson that it would be better to selectively delete the objects you want to delete, rather than relying on cla. It wouldn't need to be a cell-array, as all graphics objects share a common matlab.mixin.Heterogeneous ancestor, and thus can be mixed together in a vector.
@Ruben Kopischke: Are you using cla or cla('reset')? Would it work for your workflow to set the HandleVisibility on the proxy objects to 'off'? This will work for cla but not cla('reset').
% Objects with real data.
p = plot(magic(5));
% Create proxy objects with NaN data, which will now be rendered, but can
% be put into the legend.
hold on
pLegend = plot(NaN(5)); % Proxy objects with NaN data.
hold off
% Copy the SeriesIndex from the objects with real data to the proxy
% objects, which will make the colors and line styles match.
set(pLegend, {'SeriesIndex'},{p.SeriesIndex}');
% Setting HandleVisible to 'off' will preserve the handles across a call to
% 'cla'.
set(pLegend, 'HandleVisibility', 'off')
% Use the proxy objects in the legend.
legend(pLegend);
% Now you can call 'cla' and still see the items in the legend.
cla
Thank you all for your responses! I agree that my method is probably not that elegant, but with the last comment from @Benjamin Kraus I managed to make it work! Thanks a lot!

Sign in to comment.

More Answers (1)

If you explicitly set the axis of the plot, then you can hide objects outside the visible are and use their handles for the legend. That way it doesn't matter what objects are still "alive" in your active plot. This is a fairly cludgy way to get around this, but I've found it useful to explain the meaning of different line-widths, line-styles when they display different properties across all lines.

1 Comment

Thats true, but if you delete the object from the axes, eg. with cla, the legend is updated, even if "AutoUpdate" is set to "off".

Sign in to comment.

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!