How to plot from a line array?

조회 수: 7 (최근 30일)
Andrew Boggiano
Andrew Boggiano 2020년 2월 25일
편집: Guillaume 2020년 2월 25일
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에서 MATLAB Mobile Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by