Effacer les filtres
Effacer les filtres

Cannot delete text added to scatter plot points

17 vues (au cours des 30 derniers jours)
Matt
Matt le 30 Juil 2015
Modifié(e) : dpb le 30 Juil 2015
Hello!
I have an issue regarding TEXT items. I have a scatter plot which works fine, then I am adding to the points some simple labels using the TEXT function. It works fine, but at some point I want to update the plot which means items are removed from the scatter plot.
When I use the TEXT function again, it adds labels to the updated points, but keeps the old text in place. There is no point on the graph because it has been deleted, but the label remains although obsolete.
Using the DELETE function didn't solve the issue.
Here is my code for the scatter plot and the text labels:
set(panel3.pl1, 'XData', pointM(:,1), 'YData', pointM(:,2), 'SizeData', sizedata, 'CData', colordata);
legend1=text(pointM(:,1), pointM(:,2), c, 'Color', 'g', 'FontWeight', 'bold');
Then what I am doing is just deleting the legend1 item using a pushbutton, but it doesn't work.
Any clue?
  1 commentaire
dpb
dpb le 30 Juil 2015
If it's in a GUI; I'm guessing that most likely problem is that you haven't passed the handle legend1 in a way such that it is available to the GUI. What does "doesn't work" mean, precisely? IOW, what error or other symptoms are there? Have you used debugger to step thru the callback function?

Connectez-vous pour commenter.

Réponses (2)

Star Strider
Star Strider le 30 Juil 2015
I don’t know what about delete didn’t work for you, since we don’t have enough of your code to experiment with it.
For an example, see if this (archive) code snippet (that deletes the label for point 10) suggests a solution:
N=20;
area=100;
X=rand(1,N)*area;
Y=rand(1,N)*area;
for i=1:N
hp(i) = plot(X(i),Y(i));
ht(i) = text(X(i),Y(i),num2str(i),'fontsize',10);
hold on
end
XY=[X;Y]';
Q10 = XY(10,:); % Get Coordinates Of Point To Be Deleted
delete(hp(10)) % Delete Plotted Point
delete(ht(10)) % Delete Text Label
  2 commentaires
Muthu Annamalai
Muthu Annamalai le 30 Juil 2015
I was going to add the following comment, but it is superfluous in the light of relativity of simultaneity ... :-)
@Matt, @dpb - I wonder if you make the figure current via
fig_handle = scatter(...)
figure(fig_handle)
delete(txt_handle)
it should be able to delete the object.
dpb
dpb le 30 Juil 2015
Modifié(e) : dpb le 30 Juil 2015
Whether the figure is in focus or not makes no difference; the handle refers to the object directly without reliance on any other hierarchy.
I don't follow at all why OP would think there's even any possibility he can delete the object using its handle if he doesn't pass that handle to a new scope (the second button's callback function that tries to do the dirty).

Connectez-vous pour commenter.


Matt
Matt le 30 Juil 2015
Modifié(e) : Matt le 30 Juil 2015
Basically I have 1 pushbutton that adds points to the pointM matrix, as well as the color and size parameters for the scatter plot, and then another pushbutton that remove points from that pointM matrix. I can successfully see the points being added and removed from the scatter plot.
When I add points, the labels are added accordingly which is good, and when I remove points, the labels are updated accordingly.
Thing is that when some points are deleted, the labels are not so I end up having labels next to nothing as the point has been deleted.
I shouldn't have to pass the legend1 text handle from one button to the other since I just want to delete it and then re-create it.
Hope it helps!
  1 commentaire
dpb
dpb le 30 Juil 2015
Modifié(e) : dpb le 30 Juil 2015
"... 1 pushbutton that adds points ... then another ... that remove[s] points...I shouldn't have to pass the legend1 text handle from one button to the other since I just want to delete it ..."
How is the delete button to know what to delete and get access to it if it doesn't have the handle? Of course you have to pass it (or have access to it by some other method such as (ugly) making it global).
ADDENDUM
OK, there is a way other than passing; use findobj or similar technique in the "delete" callback routine to uncover the handle of the desired text object. This'll entail having some way to ascertain "who's who in the zoo" of all the possible text entities that may be present to make sure it's the right one (like keeping a tag or somesuch).
But, however you do it, you must somehow have the handle associated with the specific text object you wish to remove in the context of the function doing the deleting. That you make it in some other function is immaterial once you leave that function you're operating in a new scope and any and everything that was defined inside that function is gone when you leave unless it was made persistent or you passed it back out as a return value or made it global (again, Ugly!) or whatever...

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by