How to delete line in GUI axes with multiple lines?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mischa Klene
le 15 Nov 2018
Commenté : Mischa Klene
le 15 Nov 2018
I have got a GUI with checkboxes and an axes. The checkboxes load in data which is then plotted into the axes. When the checkbox is untagged, I want the line to be deleted. In the code below, the l_1 is plotted and I tried to delete l_1 after, but it does not know what l_1 is. How can I delete line l_1 from the axes?
Value_left_col1 = get(hObject,'Value');
if Value_left_col1 == 1
data_left = xlsread('Axes_left_data');
data_left_col1 = data_left(:,1);
l_1 = plot (handles.test,data_left_col1);
else
delete(l_1)
end
0 commentaires
Réponse acceptée
Adam Danz
le 15 Nov 2018
Modifié(e) : Adam Danz
le 15 Nov 2018
The reason why the code doesn't remember the handle to l_1 is because once the callback function is finished, the variables no longer exist in memory.
One solution is to make l_1 a persistant variable. At the top of the callback function, include the following line.
persistent l_1
A second solution is to tag the object and then search for the tag.
% plot with tag
l_1 = plot (handles.test,data_left_col1, 'tag', 'MyUniqueTagName');
% search for tag and delete
l_1 = findobj('tag', 'MyUniqueTagName');
delete(l_1) %this will delete ALL objects with this tag
Plus de réponses (1)
Nick
le 15 Nov 2018
I am assuming this is inside a function, once the function is completed all variables that are not part of the output (or global) will be lost, so if your function output has handles or have some global struct holding your data assign l_1 to that variable and then you will be able to delete it.
It looks a bit unnecessary to delete the line plot completely if no new data is loaded, you could just assign the 'Visible' property of the plot to 'off' or 'on' depending on the checkbox value.
A less clean way is to check for the lineplot inside the children of the axes object where you are displaying it in and deleting this handle. Depending on how many children this axes object has this can be a simple but not very clean solution.
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!