How to plot from a line array?

52 次查看(过去 30 天)
I'm indexing several lines in a line array as such:
h(i) = plot(B, sim/max(sim));% i = integer
Is there a function that is sort of the reverse of delete(h(i))?
I'm trying to add certain lines to figures together pulling from an array of several lines.
EDIT:
Basically I am looking for a way to easily replot lines that I have already plotted without having to use the plot() function and set the linewidths and colors, etc. all over again. I have found that indexing my lines in a line array has been helpful for removing certain plots by using delete(h), but I haven't found a way to re-add them onto figures after deleting.
  2 个评论
darova
darova 2020-2-25
The question is unclear. Can you explain more?
Andrew Boggiano
Andrew Boggiano 2020-2-25
Yeah, sorry, updated maintext.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2020-2-25
编辑:Guillaume 2020-2-25
Note that what you get out of plot are handles to the lines, not the line objects themselves. In normal circumstances the difference doesn't matter, but if you call delete(h) you destroy the underlying line object and you're left with a handle to an object that is no more. At this point, there's no way to restore it.
If the underlying object still exist (i.e. you haven't deleted it explictly with delete or implicitly by destroying its parent axes (e.g. with a new plot or by closing the figure), you can copy it in a new parent with copyobj.
  3 个评论
Andrew Boggiano
Andrew Boggiano 2020-2-25
I see, thank you. For some reason I had thought the data was preserved but I must have assigned that handle to another line after deleting it on accident.
Follow up, is there an easy way to remove a line from a spectrum while preserving its data?
Guillaume
Guillaume 2020-2-25
编辑:Guillaume 2020-2-25
It's not something I'd ever tried, but it turns out that you can reparent a graphics object to an empty graphics placeholder and later reparent it to a new axes. To be honest, I was surprised it works so I'd be wary of how reliable this is:
%hlines: an array of Line objects
%eg
figure; hlines = plot(magic(3));
[hlines(1:2).Parent] = deal([]); %effectively removes the first two lines from the plot.
%can then later on be reparented
figure; hax = gca;
[hlines(1:2).Parent] = deal(hax);
Note that if you're dealing with scalar line objects, you don't need to bother with [] and deal:
%hline: a scalar object
hline.Parent = [];
hline.Parent = ax;

请先登录,再进行评论。

更多回答(1 个)

darova
darova 2020-2-25
Here is an example
clc,clear
% create some data with handles
h(1) = plot([0 1],[0 1],'r');
hold on
h(2) = plot([1 3],[1 2],'b');
h(3) = plot([3 4],[2 3],'g');
hold off
% save data fro h(2)
x = get(h(2),'xdata');
y = get(h(2),'ydata');
axis tight
hold on
pause(1)
delete(h(2:3))
pause(1)
plot(x,y)
hold off
Why do you need such function? Can you show your code?

类别

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