Remove a line from a plot in UIAxes App Designer

조회 수: 38 (최근 30일)
Dario Fiumarella
Dario Fiumarella 2020년 10월 13일
댓글: Prajwol Tamrakar 2023년 8월 9일
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
Prajwol Tamrakar
Prajwol Tamrakar 2023년 8월 9일
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!!

댓글을 달려면 로그인하십시오.

채택된 답변

Cam Salzberger
Cam Salzberger 2020년 10월 13일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by