How to delete line in GUI axes with multiple lines?

10 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 15 Nov 2018
Edited: Adam Danz on 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

More Answers (1)

Nick
Nick on 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.
  1 Comment
Mischa Klene
Mischa Klene on 15 Nov 2018
Thank you for your answer. It is indeed inside an function, I also tried to find out how to set the visibility of the line. I used the line seen below, but then it would still not know the properties of l_1.
set(l_1,'visible', 'off');
When I use the solution from Adam Danz, it works with 'delete' and setting the visibility.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!