Getting handles of lines and deleting last 2 line plots

6 views (last 30 days)
Hello. I am plotting some raw data from a spectrometer onto a UIAxes and a smoothed version of this data. So for each data curve, there are 2 plots.
I often have about 10 plots of raw data (and obviously 10 smoothed plots).
I want to be able to remove the last raw data (and its smoothed plot) and have tried the code below.
However, it doesn't seem to be doing what I want it to. When I first run this (from a pushbutton callback), it only removes one line and not 2.
I can't see my error.
try
h = findobj(app.UIAxes,'Type','line')
delete(h(2)); %Delete 2nd last line object
delete(h(1)); %Delete last line object
catch
end

Accepted Answer

Steven Lord
Steven Lord on 14 Feb 2020
I wouldn't assume that the first two elements of the array returned by findobj were the last two lines created. I don't think the documentation for findobj promises that behavior. Instead I would create the lines with specific Tag property values that reflect the order or that you store the lines in order in an array that you store in a property of your app, so you can "push" and "pop" them off in order in your line deletion method.
  1 Comment
Jason
Jason on 14 Feb 2020
Edited: Jason on 14 Feb 2020
Thanks Steven, could you expand on push and pop please. I don't see them as matlab functions.
Perhaps I could just do somethign like:
hlast(1)=plot(..my raw data)
hlast(2)=plot(..my smoothed version of raw data)
setappdata(0,'lastplots',hlast)
and then in my delete function
hlast=getappdata(0,'lastplots')
delete(hlast)
?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!