Assigning a line plot object to a global property doesn't retain it after the object is deleted

4 次查看(过去 30 天)
Is there a reason why when I delete a plot of a multiplot graph and assign it to a global (public) property it doesn't retain it.
ax=app.UIAxes; % My plot is on a UIAxes
t = ax.Children(1) % Get the last plot
app.myPlot=t % myPlot is a Global property
delete(t);
app.myPlot
app.myPlot shows:
ans =
handle to deleted Line
so instead I have to access all the properties of the line individually and assign to a struct.
s = struct;
s.X=t.XData;
s.Y=t.YData;
s.Cl=t.Color;
s.DN=t.DisplayName;
s.Ls=t.LineStyle;
s.Lw=t.LineWidth
s.M=t.Marker;
app.myPlot=s;
Just seems more clunky!

采纳的回答

Steven Lord
Steven Lord 2024-6-14
All of the handles to the line object (which is a handle object) are references to the same thing on the screen. If you delete() the handle, you delete the thing on the screen. Trying to access it after it's been deleted would be like throwing away a piece of paper but hoping to be able to feel it using a picture you took on your phone. That's not going to work. A very few things do still work; asking for its class or asking if it isvalid would. But the line is gone; asking for its XData or YData won't work.
What were you hoping to have happen by deleting the line on the screen but then trying to reference it?
  6 个评论
Steven Lord
Steven Lord 2024-6-14
You can control what gets displayed in the legend by passing a vector of handles into legend.
x = 0:360;
axis([0 360 -1 1])
hold on
s = plot(x, sind(x), '-', DisplayName = "sine");
c = plot(x, cosd(x), ':', DisplayName = "cosine");
legend(s) % Show only the sine curve in the legend
Jason
Jason 2024-6-14
编辑:Jason 2024-6-14
Thanks. I know this and thats how I create my legend (thru the parameter displayName in the plot function), but when I turn off the last line visibility, the legend item reamins, but dims out.
So the purple curve is the last plot (hence is accesssed thru ax.Children(1)
and when I then set its visible to off

请先登录,再进行评论。

更多回答(1 个)

Jason
Jason 2024-6-18
Accepting Stevens answer as he did answer my original question, however I have taken the legend issue out to a new question - hope thats O.K

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by