How to remove labels from a plot?
110 views (last 30 days)
Show older comments
Goodnight. I ask for your help with a new problem. I am having problems with the removal of labels in a graph. What happens is that I have a matrix (of 3 columns) called "Nodes_et" that carries the x-y coordinates in its first 2 columns, while in the third it is named after the point to which those coordinates correspond. I have these lines of code that are helping me to generate and position the labels correctly (I leave part of the code and some images to better understand the situation I commented):
Nodos_et=unique(Nodos_et,'rows');
eti=text(Nodos_et(:,1)+0.1,Nodos_et(:,2)+0.1,num2cell(Nodos_et(:,3)) );
hold on;
As you observed the code fulfills its function, however I need to remove certain labels (depending on a condition that I have programmed) and I can not do them. Try reassigning the "Nodes_et" matrix to the values you wanted to label, for example, the values that the matrix carried minus the one in the second row remain, assuming this would eliminate the label that was in that second row, but it didn't work:
hold off;
Nodos_et(2,:) = [];
eti=text(Nodos_et(:,1)+0.1,Nodos_et(:,2)+0.1,num2cell(Nodos_et(:,3)) );
remains the same
Can anyone help me with this problem? Thank you.
6 Comments
Adam Danz
on 8 Aug 2019
I included an answer that shows how to store text object handles and how to delete them. You'll have to make some design decisions about how to best organize your code so you can keep track of which handles are associated with which rows of your UITable. That shouldn't be a problem. It looks like lines are also created/deleted based on the rows of your UITable. Text objects will work the same way as the lines.
Accepted Answer
Adam Danz
on 8 Aug 2019
Edited: Adam Danz
on 8 Aug 2019
Text objects are just like any other graphics object. When you create a new text object, store its handle so you can later delete it. Here's an example.
th = text(.5, .5,'MyText');
% now delete it
delete(th)
If you're implementing this within a GUI and the function that deletes handles is different from the function that creates them, you'll need to store the text object handles somewhere. You can use guidata() for that.
7 Comments
Rik
on 9 Aug 2019
My guess is that the NextPlot property is set to 'replace' instead of 'add'. If that is the cause you can either modify the property directly, or use hold('on') to get the same effect.
Also, always use target handles in a GUI. Almost every function allows you to set the parent object, plot is no exception.
Adam Danz
on 10 Aug 2019
- reading screenshots of code is not fun. Whenever you're sharing relevant code, copy-paste it and make sure it's formatted using the [>] button.
- sceenshots of your GUI may be helpful but I see at least 4 of them and at first glance 3 of them look identical. So instead of spending time helping with a solution, we have to spend a lot of time trying to understand what we are supposed to see. Always include a quick sentence or two (at least) such as "the image below shows....."
- Less is more (usually). Most of the time volunteers here must ask for additional code, error messages, etc. so it's good to anticipate that and include it in your question. But it's also easy to overwhelm people with too many words and images.
From what I understand, your labels are disappearing as soon as another plot() command is executed. If that's the problem, I agree with Rik and the simple solution is to merely hold your axes.
hold(app.UIAxes, 'on')
% |__________| replace this with the handle to your axes
More Answers (0)
See Also
Categories
Find more on Annotations 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!