Remove data elements of the legend from figure

724 Ansichten (letzte 30 Tage)
Marisabel Gonzalez
Marisabel Gonzalez am 9 Apr. 2019
Kommentiert: Kaveh Vejdani am 10 Jun. 2023
Hi,
I have several figures in a loop-rich script in which the information of the legend would vary for most of the figures. I was wondering if there is a way of removing data elements from the legend using figure setting options. I know I can insert/remove the legent, rename the data points, change the legend appearance, etc, but I am not sure if I can remove elements from it.
For example, in the figure below I would've like to remove the last column but not by using code, instead doing it from the figure itself.
I am aware my changes won't be saved.

Akzeptierte Antwort

Kelly Kearney
Kelly Kearney am 9 Apr. 2019
Technically, you can delete parts of a legend, but I only know how to do it via code, not via to UI.
I should mention that using the multiple-output syntax for legend is "not recommended" by the official documentation, meaning this code could easily break in future versions of Matlab... but they've been saying that since the HG2 release in 2014b and so far I've only encountered problems in a few edge cases.
Anyway, if you really want to delete parts of a legend, here's how you can:
% An example plot, 10 to-be-labeled lines and 3 extras
h1 = plot(rand(10,9), 'ro');
hold on
h2 = plot(rand(10,3), '-k');
lbl = strtrim(cellstr(num2str((1:12)', 'data%d')));
% Label everything
[hleg, hico] = legend(lbl);
% Delete objects associated with last 3 black lines
istxt = strcmp(get(hico, 'type'), 'text');
hicot = hico(istxt);
hicol = hico(~istxt);
delete(hicot(ismember(get(hicot, 'String'), {'data10','data11','data12'})));
delete(hicol(ismember(get(hicol, 'Tag'), {'data10','data11','data12'})));
The better solution would be to just not label those lines at all from the start:
% An example plot, 10 to-be-labeled lines and 3 extras
h1 = plot(rand(10,9), 'ro');
hold on
h2 = plot(rand(10,3), '-k');
lbl = strtrim(cellstr(num2str((1:12)', 'data%d')));
% Label only the desired lines
legend(h1, lbl(1:9))

Weitere Antworten (4)

EL ISMAILI Mohammed
EL ISMAILI Mohammed am 25 Apr. 2020
Hi!
For removing a legend element dirctly from the figure :
-Right click on the legend
-Open "Proprety Inspector"
-Go to: LABELS > Strings. Double clicks on the string's box
-Remove/Edit what you want
It works for me. I have the matlab version 2019a.
  2 Kommentare
Narges Raee
Narges Raee am 26 Okt. 2022
same problem as James! it is deleted the names of the objects and shift eveything else up, so not working!

Melden Sie sich an, um zu kommentieren.


Adam Danz
Adam Danz am 9 Apr. 2019
Bearbeitet: Adam Danz am 9 Apr. 2019
" I was wondering if there is a way of removing data elements from the figure itself"
If you want to remove objects from a figure that has already been produced, just click on the object and press delete. That will also remove its representation in the legend.
Another option is to selection objects by using the display name in the legend. For example, these lines below will find the handle(s) to the object(s) associated with the legend name "data1" and will delete all of those objects.
allChildren = get(gca, 'Children'); % list of all objects on axes
displayNames = get(allChildren, 'DisplayName'); % list of all legend display names
% Remove object associated with "data1" in legend
delete(allChildren(strcmp(displayNames, 'data1')))
If you want to keep the object on the figure but want to remove its representation from the legend, the only way I know of doing that is to recreate the legend using the object handles to specify legend components. We can get the text in the legend and delete or edit that but we do not have access to the handles of the legend components. Most of the time, the figure can be recreated and that's the much easier approach. But if you've got a figure and no code to reporduce it, you'll have to obtain the handles of all graphic objects and then recreate the legend.
  5 Kommentare
Payam Razavi
Payam Razavi am 24 Feb. 2023
% Thanks for this comment, it was really helpful
% If axes cannot be deleted (invisible axes) use:
legend(app.UIAxes,allChildren(~strcmp(displayNames, '')),'location','Best')

Melden Sie sich an, um zu kommentieren.


HiWave
HiWave am 31 Mai 2023
Bearbeitet: HiWave am 31 Mai 2023
Although an answer has been accepted, I'm going to add to the discussion. I too faced a similar circumstance. I found the easiest method was to select the data I wanted to keep, then cut. Delete all the rest of the data and legend, then paste the data back into the plot. Add a new legend and rename the entries. Not exactly efficient, but got the job done. No code necessary.
  2 Kommentare
Kaveh Vejdani
Kaveh Vejdani am 10 Jun. 2023
Thank you HiWave! You're right, the ultimate answer is :
L.AutoUpdate = 'off';
Saves the day!

Melden Sie sich an, um zu kommentieren.


Peter Beringer
Peter Beringer am 24 Aug. 2021
There's a very simple way if you're happy to do it in the Figure window itself (you can also go File > Generate Code if you wish to include the settings in the code directly). You're correct that editing the text string in the Properties Inspector is going to leave the unwanted data in the legend, just without a name. Easiest way to do it from within the figure is to go to the top of the Property Inspector window, click the ">" arrow next to "Figure" to open up a drop-down menu, then Apple/command-click or right-click the data you don't want and select "DELETE". See attached screenshot (obviously yours will have different labels, etc.) Hope I've explained that okay.
Happy MATLABBING! :)
  1 Kommentar
SIMONE GRANDINETTI
SIMONE GRANDINETTI am 29 Nov. 2022
I tried this but it deletes the data from the plot as well (i was trying to plot a yline alongside a set of curves but i didn't want it to appear in the legend).

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by