Remove a line from a plot in UIAxes App Designer

52 views (last 30 days)
Hello Everyone,
I would like to add or remove a line from a UIAxes, using the Switch Button.
When the switch button is on, everything is working and the curve is plotted. But when I switch to off, the line still remains, and it is not removed from the graph.
In the UIAxes toolbar, I have set the Multiple Plots settings in this way:
NextPlot: add; SortMethod: childorder
This is the code I used, but it is not working:
Thank You!!
function SperimentaleSwitchValueChanged(app, event)
value = app.SperimentaleSwitch.Value;
if strcmp(value, 'On')
exp = importdata('...\Sperimentale.txt');
exp_force = exp(:,2);
exp_displ = exp(:,1);
h1 = plot (app.UIAxes, exp_displ, exp_force/1000)
end
if strcmp(value, 'Off')
delete(h1)
end
end
  1 Comment
Prajwol Tamrakar
Prajwol Tamrakar on 9 Aug 2023
See app.UIAxes.Children!! Each plot you created will be included here. The latest plot will be included on top. If you have four line plots, you can delete the first one by
delete(app.UIAxes.Children(4))
Similary, to delete the latest plot, use
delete(app.UIAxes.Children(1))
Hope this is helpful!!

Sign in to comment.

Accepted Answer

Cam Salzberger
Cam Salzberger on 13 Oct 2020
Hello Dario,
The misunderstanding here seems to be that everytime this function is being called, the workspace inside of the function is fresh. So the first time you call it, when the switch is moved to "on", you create the variables "exp", "exp_force", "exp_displ", and "h1". Then when the function returns, these variables go out of scope. The line on the plot will still exist because it has been added as a graphics object, and is referred to in other locations (e.g. Children property of the axes), but the variable "h1" no longer exists to refer to it. So the next time you call the function and it tries to run "delete(h1)", I would expect an error to occur because "h1" isn't an existing variable.
If this is a big feature of your app, I would suggest adding a property that keeps the line handle for you. Then you can simply update the data in the line handle, which is actually more efficient than deleting and recreating the line. You can create the line object with empty or NaN data points in the app constructor, and then just set the XData and YData properties of the line in the switch callback (to the data from the file if "on", or back to empty/NaN if "off").
Alternatively, though less preferred, you could make "h1" a persistent variable. That would allow it to be referred to between calls. That's less robust (if the callback gets disabled temporarily, called out of order, or you change the default setting of the switch), but it would probably be simple to implement and should work for a simple application like this.
-Cam

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!