How can I delete an xline/plot from a plot without deleting the other ones on the graph? (I am using checkbox)

72 views (last 30 days)
Good morning,
I am currently using MatLab App Designer to create an app and I am using axes for my plot and checkboxes on the side to make plot/xlines appear. I have a function for each one of my checkboxes so that if i check one of them, that specific line will be on the graph but not the other. If I check multiple boxes, I will have my eslected lines on the plot but once I turn one of my checkboxes off, it deletes all of my line, even if my other checkboxes are still checked. here is an example of one of my checkboxes:
function FT4CBValueChanged(app, event)
value = app.FT40CB.Value;
p40 = plot(app.CoVPlot, [40 40], [0 500], "Color", "r", "LineWidth", 2);
if value == 1
str40 = "CoV @ 40'" ;
plot(app.CoVPlot, p40)
text(app.CoVPlot, [40.5 40.5], [40 40], str40)
else
cla(app.CoVPlot)
yline(app.CoVPlot, 15, "b", "LineWidth", 2)
yline(app.CoVPlot, 10, "b", "LineWidth", 2)
end
end
To clear my line, I am using cla (i know it is to clear the axes, zhich is zhy all of my other plotted lines are disappearing, but that it the only thing that make me delete my lines from the plot).
Hopefully it makes sense!
Thank you for the help!

Accepted Answer

Cris LaPierre
Cris LaPierre on 13 Jun 2023
Yes, cla clears everything on the axes. Instead, delete the handle to your line. See this answer: https://www.mathworks.com/matlabcentral/answers/21006-how-to-erase-a-line
To work, you will have to save your line handle as an app property. See here: https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
  3 Comments
Cris LaPierre
Cris LaPierre on 13 Jun 2023
I'm attaching a simple example. If you can plot the line, you can remove it using delete as long as you have captured the line object as an app property. See the attached app.

Sign in to comment.

More Answers (1)

dpb
dpb on 13 Jun 2023
You need to keep each line handle when it is plotted as a global app data variable for each CB that plots each line. Then the callback for the CB that is to unset that particular line uses that particular handle -- it could either delete the line completely or just toggle the 'Visible' property 'on','off' if the user can expect to turn it back on again...
  1 Comment
Margaux
Margaux on 13 Jun 2023
Thank you for your answer! I have also tried that but it seems like even though the "visible" property is "off", we can still see the line...

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!