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 个评论
采纳的回答
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 个评论
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
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?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!