How to delete line in GUI axes with multiple lines?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Adam Danz
2018-11-15
编辑:Adam Danz
2018-11-15
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
更多回答(1 个)
Nick
2018-11-15
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!