How to remove labels from a plot?

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;
1.png
2.png
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)) );
3.png
4.png
5.png
remains the same
6.png
Can anyone help me with this problem? Thank you.

6 comentarios

Rik
Rik el 8 de Ag. de 2019
You mean you want to remove some of the numbers you're creating with the text function?
Pedro Guevara
Pedro Guevara el 8 de Ag. de 2019
Exact. I need to remove certain tags that had already been created and generated thanks to my array .
Adam Danz
Adam Danz el 8 de Ag. de 2019
Editada: Adam Danz el 8 de Ag. de 2019
Maybe I'm not understanding the quesiton but why can't you just comment-out (or delete) the text() functions that are writing the labels in the first place?
If you need the labels temporarily, you can delete them using their handles.
th = text(. . .)
delete(th)
Adam
Adam el 8 de Ag. de 2019
Always keep hold of the handles to the graphics objects you create if you think you will later want to edit them in some way, it is a lot easier than messing around with having to find them again afterwards.
As Adam Danz says though you have to explicitly delete text objects, you can't just call text again with fewer elements and expect that it will replace all the ones you did previously with the new smaller list.
Pedro Guevara
Pedro Guevara el 8 de Ag. de 2019
Editada: Pedro Guevara el 8 de Ag. de 2019
Thank you very much for the reply. My programming is made so that when certain conditions are fulfilled, certain labels must be removed, these labels must be created and removed in compliance with the conditions that I have programs in the uitable that you see in the images. I leave this video to understand a little better how it works or what I intend to do:
The rows that are deleted from the tool must also delete the corresponding "nodes" (columns 4 and 5) that existed in the plot. This video was before it included the tags.
Could he ask for your help? I am not very aware of how is the handling of the - Function Handles -. Will it be very annoying if I ask you for an example of how it would be for the situation I am facing right now?
Adam Danz
Adam Danz el 8 de Ag. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 8 de Ag. de 2019
Editada: Adam Danz el 8 de Ag. de 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 comentarios

Thank you. effectively with your lines I manage to eliminate the labels. But I have another doubt, my variable "eti" is saving these tags in an array of type -text-, as shown in the following figure:
Nodos_et=unique(Nodos_et,'rows');
eti=text(Nodos_et(:,1)+0.1,Nodos_et(:,2)+0.1,num2cell(Nodos_et(:,3)) );
eti(2,1);
hold on;
1.png
¿How can I capture these tags as an array of type -number- like -double-?
Thank you
Adam Danz
Adam Danz el 8 de Ag. de 2019
Editada: Adam Danz el 9 de Ag. de 2019
I don't understand the question. You can convert a text object handle to double as shown below but I doubt that's what you want to do. It doesn't serve any purpose to do this.
th = text(. . .);
thDouble = double(th); % But why???
Pedro Guevara
Pedro Guevara el 9 de Ag. de 2019
It served me, thank you very much. But now I have a new doubt. I do not know if it is better to consult you through this post or create a new one. Thank you.
Adam Danz
Adam Danz el 9 de Ag. de 2019
Ha! If your new question is related, I don't mind continuing the discussion at all :)
If your new question is not related, it may serve you better to make a new question that will likely be seen by a wider audience. There are so many talented volunteers here with a vast range of expertese. I only fulfill a tiny niche.
It certainly has a relationship.
The doubt is the next one. When I run my program (after it has already put some labels) and it goes through the lines of code:
datos=str2double(t2); % "t2" is a variable of type cell that only contains the data that contains the data, fulfills an orthogonality condition that I have programmed for my uitable.
M= datos (:,6:9);
M([CelDig(1,1)],:)=[] ; % "CelDig (1,1)" is a value of a vector that saves the last position that was typed in the uitable. This line allows me to eliminate the row that does not meet my orthogonality condition
x=[M(:,1),M(:,3)];
y=[M(:,2),M(:,4)];
plot(x',y'); % Here the problem occurs, all my tags are deleted.
All my tags will be deleted, which I don't want since I have programmed a code that selectively removes the tags that I want to remove (read the comments that are the following lines of code):
fila_comp=V_org(celdas,1); % Corresponds to the ind of the row in my uitable that contains the nodes that I want to remove from the tags
%-----------------------------------------------
B=datos (fila_comp,4:5); % "B" is a vector that stores the values ​​of the uitable (in columns 4-5)
for j=1:length(Nodos_et(:,3)) % "Nodos_et" is the matrix that carries my coordinates with its corresponding node
x(1,j) = str2double ( eti(j).String ); % "eti" is my variable of type -text array- that stores the labels of the introduced nodes.
end
A=(Nodos_et(:,3))';
for j=1:length(B)
if sum( find(x==B(1,j)) ) >=1
iry(j,1)=find(x==B(1,j)) ;
irx(j,1)=find(A==B(1,j));
delete( eti(iry(j,1),1) );
end
end
eti([iry],:)=[]; % this line allows me to remove the rows from my vector "eti" that contained the nodes of the row that I want to delete ("row_comp")
Nodos_et([irx],:)=[]; % this line allows me to remove the rows from my array "Nodos_et" that contained the nodes of the row that I want to delete ("row_comp")
I leave images to better exemplify the situation, since it is a bit complicated to understand what I am trying to do:
1.png
2.png
3.png
4.png
5.png
6.png
7.png
8.png
9.png
10.png
11.png
Here all my tags are lost
12.png
Thank you. I hope you understand the subject quite well. I know it's complicated. :(
Rik
Rik el 9 de Ag. de 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.
@Pedro, a few quick tips unrelated to your question;
  • 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Labels and Annotations en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

el 8 de Ag. de 2019

Comentada:

el 10 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by