How can I delete a plot among many other plots?

Hello. I tried to find an answer for this but the findobj didn't work for me.
load cities.mat
figure(1)
for i=1:9
plot(ratings(:,i),'DisplayName',num2str(categories(i,:)),'LineWidth',1)
hold on
end
lgd=legend('show')
ylim([0*10^4 6*10^4])
xlim([0 size(ratings,1)])
set(gca,'xtick',[0:50:300])
set(gcf,'position',[360 278 800 600])
title('Cities Data')
xlabel('Cities')
lgd.Position=[0.7134/1.07 0.6464/1.2 0.1741*1.3 0.2548*1.3]
I have different DisplayNames i.e climate, arts etc... and I want to delete the arts plot after using this code. How can I do that?
Thanks, Eran.

 Accepted Answer

Stephen23
Stephen23 on 14 May 2017
Edited: Stephen23 on 14 May 2017
The cause of this is simple: categories is a 9x14 char array. In the loop you use each row of this as the DisplayName property. This means that each DisplayName name is a 1x14 char vector (even if only the first few characters are visible). And then you use findobj search for a 1x4 char vector, which clearly does not match any of the 1x14 char vectors that you used as DisplayNames because it has a different size. Simply put,
'arts '
does not match
'arts'
as indeed it should not!
Some solutions:
  • use strtrim before allocating the DisplayName.
  • use cellstr on categories and obtain the DisplayName from that cell.
  • pad your search name so that it has size 1x14.
  • use indexing to extract the original 1x14 char from categories (you have already tried this!).
  • etc,etc
Bonus (ignore this if you are not in the mood for a didactic rant): this is a good example of why actually looking at what code is actually doing is important. Many beginners rely on what they believe the code is doing, which is not always the same thing... code does not care what you want/assert/assume/believe/..., it will do exactly what it is written to do. So when debugging and even during writing of code, it is worthwhile forgetting what you assume, and start to look at the code itself: forget that you want to match 'arts', and start much more general: is there a variable, what class is it, what size is it,...
A surprisingly large number of bugs that beginners ask about on this forum would be solved by taking a good look at the outputs (or inputs) to their functions and operations. Often they have assured us repeatedly that some variable is of the correct size/class/has non-zero values/whatever..., but when it is checked very simply using size, class, or something similar, we find that in fact it does not.
Moral of the story: assume nothing!

More Answers (1)

delete( findobj(gcf, 'Name', 'arts') )

9 Comments

Hi. it's still not working and I only get an empty holder. If I write get(gcf) - I see that 'Name' is empty. I guess it is maybe because of the command DisplayName. if it is because of the DisplayName - Any other suggestion how to add a legend name to each element of the loop?
Thanks
delete( findobj(gcf, 'DisplayName', 'arts') )
still not working. when i try to do
findobj(gcf, 'DisplayName', 'arts')
it gives an empty graphic holder.
UPDATE: I managed to remove the line I wanted using this command:
children = get(gca, 'children');
delete(children(3));
Now I need to remove the legend label of it. How can I do that? looking at the Legend Properties , I see that it might be connected to the string, but not sure.
Any suggestions?
Thanks!
R2017a:
L = plot(rand(1,10),'DisplayName','frodo');
hold on; L2 = plot(rand(1,10),'DisplayName','frodo2')
legend('show')
pause(10)
delete( findobj(gca, 'DisplayName','frodo') )
and away goes the item, and the legend for it as well.
The legend entry going away automatically is new in R2017a. In previous versions,
legend('show')
would probably re-create the legend.
your code works. but for some reason it doesn't work with my figure. any idea why? the only difference i can notice is the plot been created by a loop.
"I have different DisplayNames i.e climate, arts etc"
No, you do not: you have
plot(ratings(:,i), 'DisplayName', num2str(categories(i,:)), 'LineWidth', 1)
which is converting a number to a string and using that as the DisplayName. So like '17' for example. Not 'arts'
Ok first of all the num2str is useless in this point since the result is a string anyway. so i removed the the num2str and put just categories(i,:). But it's still not working... when I try to do
findobj(gca,'DisplayName', 'arts')
I don't get anything. even if i put a number instead of the arts still nothing happens .
UPDATE: ok so when I do:
findobj(gca,'DisplayName', categories(7,:))
This way it will delete the arts plot. But I don't get it why it is like that? is there another way I can do by just writing the name of the category?
Stephen23
Stephen23 on 14 May 2017
Edited: Stephen23 on 14 May 2017
@Eran Sandman: it finds the last category because i has the value of the last loop iteration.
In any case, see my answer for an explanation of your problem, and how to fix it.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!